## Object oriented programming – Computer Science Stack Exchange

Thanks for contributing an answer to Computer Science Stack Exchange!

• Please be sure to answer the question. Provide details and share your research!

But avoid

• Asking for help, clarification, or responding to other answers.
• Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.

To learn more, see our tips on writing great answers.

## programming languages – I want to learn more about computer science. Where should I start?

Hello computer scientists,

I am a mathematician. I have taken some undergraduate courses in C++, Python, assembly language, boolean algebra, logic, graph theory, etc. I would like to learn more about computer science because I think it’s cool. Could you give me some advice as to where to start? Thanks!

## functional programming – How to check if a string is null and use its value avoiding NullPointerException in Java

I need to map few object’s properties to another object but those properties could also have null values.

This can cause NullPointerExceptions that can be avoided using this code:

private static String nullOrString(Object o){
return o==null?null:o.toString();
}


Can this function be improved / made more robust in modern Java?

## linear programming – Which topics of mathematics should I need to learn to be a good app developer?

I’m 29 years old. I couldn’t continue my studies after grade 10 due to some financial issues and I didn’t have time to practice mathematics. It’s been more than 11 years since I left studies. Now I want to continue in the field of programming. I have grasped the knowledge of basic programing concepts like variables, data types, conditional statements and loops etc. I guess the next stages are data structures and algorithms. Here the lack of math skills will make it difficult for me understand the concepts. I want to know which topics of mathematics I need to learn to be able to program for mobile app development. Can anyone help me, please?

## procedural programming – My code breaks as soon as I try to print a variable

EDIT: I think I figured out what was wrong. Since I was adding in Print(m) AFTER I ran the loop I had to clear my variables if I wanted to run the loop again. I guess my new question is, why did I have to clear m and x? I tried to rerun F(x_) to reset the function and I tried to rerun m=3 after but it just gave me strange results which I will attach below.

Original issue starts here
I’m running a For loop(and it needs to be a for loop) thats supposed to do newtons method on a function.
I’ll post the parts of the code that I think are needed to get the error

F(x_) = x^2 Cos(x) + x + 3
m = 3

For(i = 0, i < 5, i++,
F'(m);
y = F(m) + F'(m) (x - m);
x = (0 - F(m))/(F'(m))   + m;
Print(N(F(x)));
m = x


All of this code here works fine. The moment I add Print(m) or Print(x) in the loop it completely breaks and I do not know why.
The picture below is the code working BEFORE I add in Print(m) anywhere in the loop.

## programming languages – Looking for a software to make license key for my pdf file such a way that for different machines the license key will be different

Looking for a software to make license key for my pdf file such a way that for different machines the license key will be different. I have already seen serial key maker is not a solution. I am describing it a little more. Whenever someone will open the pdf file, there will be generated as for example 16 digit no and when he/she will email me that copied out 16 digits no and then I will enter the no in my software there will be generated a serial key for his machine only. so obviously you got my point for the different machines the 16 digits no will be different.

## functional programming – Doesn’t “Always test through the public interface” contradict testing of individual composed functions?

I’m currently reading “Composing Software” by Eric Elliott, which is about functional programming in JavaScript. He states that if you compose multiple functions together, and that these functions have been fully tested in isolation – then you don’t need to unit test the composed function (as that can be done using an integrated test that performs all the side effects.)

But isn’t simply testing the individual functions violating the core TDD principle of “always test the public inteface, and not the implementation”? Arguably the composing of the smaller functions is an implementation detail. As long as my service performs the action that it says it will do, we shouldn’t really care about the small “private” functions being composed in order to get the right behaviour.

I’m trying to reconcile the two ideas but I’m struggling. The only way around this (that I can think of) is to mock the dependencies that my service needs so that it can be tested through the public interface, but going functional was my attempt to stop using mocks in the first place.

## python – K Group Clusterization programming challenge from justpaste.it

I have implemented k means as below, and as far as I know, it doesn’t guarantee k grouping when given k. But the task I have which is https://justpaste.it/5cmte, requires me to return exactly k clusters. How should I go around that?

## Task

You are given n points.
You have to separate them into k groups (clusters).

## Input:

The first line contains two numbers: n – the number of points and k – the number of groups.
The next n lines contain two numbers x and y – the coordinates of a point.

## Output:

The first line should contain the sum of distances between each point to the center of its cluster.
Each of the next k lines should contain the coordinates of center of the current cluster, the number of points of the cluster and the indices of the points belonging to the cluster (all values should be separated with a whitespace).

Example input:

6 3
1 2
5 5
7 3
2 4
9 9
3 6

Output:

10.8982
1.5 3 2 1 4
3 6 1 6
7 5.66667 3 2 3 5

Sometimes, the same input, gives k-1, k-2 clusters instead of k.

So my point is that. k means does not guarantee k clusters. I am required to return exact k clusters, how should I do that?

K means:

import sys, random, math
from collections import defaultdict

def pointAvg(points):
dimensions = len(points(0))
newCenter = ()
for dimension in range(dimensions):
dimSum = 0
for p in points:
dimSum += p(dimension)
newCenter.append(dimSum / float(len(points)))
return newCenter

def updateCenters(dataSet, assignments):
newMeans = defaultdict(list)
centers = ()
for assignment, point in zip(assignments, dataSet):
newMeans(assignment).append(point)
for points in newMeans.values():
centers.append(pointAvg(points))
return centers, newMeans

def assignPoints(dataPoints, centers):
assignments = ()
for point in dataPoints:
shortest = float('inf')
shortestIndex, distances = 0, 0
for i in range(len(centers)):
val = distance(point, centers(i))
distances += val
if val < shortest:
shortest = val
shortestIndex = i
assignments.append(shortestIndex)
return (assignments, distances)

def distance(a, b):
dimensions = len(a)
_sum = 0
for dimension in range(dimensions):
differenceSq = (a(dimension) - b(dimension)) ** 2
_sum += differenceSq
return math.sqrt(_sum)

def generateK(dataSet, k):
centers = ()
dimensions = len(dataSet(0))
minMax = defaultdict(int)
for point in dataSet:
for i in range(dimensions):
val = point(i)
minKey = 'min_%d' % i
maxKey = 'max_%d' % i
if minKey not in minMax or val < minMax(minKey):
minMax(minKey) = val
if maxKey not in minMax or val > minMax(maxKey):
minMax(maxKey) = val
for _k in range(k):
randPoint = ()
for i in range(dimensions):
minVal = minMax('min_%d' % i)
maxVal = minMax('max_%d' % i)
randPoint.append(random.uniform(minVal, maxVal))
centers.append(randPoint)
return centers

def kMeans(dataset, k):
kPoints = generateK(dataset, k)
assignments = assignPoints(dataset, kPoints)(0)
oldAssignments = None
while assignments != oldAssignments:
updatedCenters = updateCenters(dataset, assignments)
newCenters = updatedCenters(0)
means = updatedCenters(1)
oldAssignments = assignments
assignedPoints = assignPoints(dataset, newCenters)
assignments = assignedPoints(0)
sumDistances = assignedPoints(1)
print(sumDistances)
return (newCenters, means)

if __name__ == '__main__':

# sys.stdin  = open('input.txt', 'r')
# sys.stdout = open('output.txt', 'w')

i, points = 0, ()
for line in sys.stdin:
line = line.rstrip()
line = line.split()
line(0), line(1) = int(line(0)), int(line(1))
if (i == 0):
n, k = line(0), line(1)
else:
points.append(line)
i += 1

clusters = kMeans(points, k)
for i in range(k):
print('%g' % clusters(0)(i)(0), '%g' % clusters(0)(i)(1), len(clusters(1)(i)), end=' ')
for j in range(len(clusters(1)(i))):
print(points.index(clusters(1)(i)(j)) + 1, end=' ')
print()


## programming languages – key benefit of using container?

I’m not a guy who has computer science related background, just curious about the container technology and of course I did some googling to see what container is.

hope this is the correct site for container related question.

Here are my questions.

1. what is the primary benefit that container tech. offer? packing the environment(libraries, configurations…etc) for codes/programs or offering great portability across different computer system?

(some guy tells me that the portability isn’t the key benefit but close to)

1. is it doable and practical to use container provide services just like what servers can do?

2. can containers isolate malicious codes/apps to prevent them from affecting underlying operating system?

## history – What are programming languages written in?

What are programming languages written in?

Programming language compilers and runtimes are written in programming languages — not necessarily languages that are older or are different than the one they take as input.  Some of the runtime code will drop into assembly to access certain hardware instructions or code sequences not easily obtained through the compiler.

Once bootstrapped, programming languages can self-host, so they are often written in the same language they compile.  For example, C compilers are written in C or C++ and C#’s Roselyn compiler is written in C#.

When the Roselyn compiler adds a new language feature, they won’t use it in the source code for the compiler until it is debugged and working (e.g. released).  This akin to the bootstrapping exercise (limited to a new feature rather than the whole language).

But to be clear, there is the potential (and often realized) for the programming language to be written in the latest version of its input language.

So what came first, and what was that coded in?

Machine code came first, and the first assemblers were themselves very very simple (early assembly languages were very easy to parse and generate machine code for), they were written in machine code, until bootstrapped and self-hosted.