[Solved] Find next Aloha Number

Despite my comment above, I wondered about the “time and space complexity”. It turned out to be trivial; this only takes 3 long integer variables and a single loop over each input digit (which can be incremented by this algorithm to use 1 more digit; and this will not cause an integer overflow). Starting from … Read more

[Solved] pattern drawing programming logic [closed]

A C++ implementation : Without any formatting void print(int n) { for(int i=n, cl=1, cr=n*n+1; i>0; cl+=i, –i, cr-=i) { for(int j=0; j<i; ++j) cout << cl+j; for(int j=0; j<i; ++j) cout << cr+j; cout << endl; } } With dashes and stars void print(int n) { for(int i=n, cl=1, cr=n*n+1; i>0; cl+=i, –i, cr-=i) … Read more

[Solved] Java loops with math symbols [closed]

From what I could make out of your question, I took your code made it compile and modified it: public class test { public static void functionC(int n) { for(int i = 0; i < n; i++) { System.out.println(“Hello world”); } } public static void main(String[] args){ functionC(5); } } This would be functionC. For … Read more

[Solved] How do I make each line from a file list into separate variables?

first, read the file into a string line by line; then convert the string to a variable. In python, you can do the following i = 0 with open(“file.txt”, “r”) as ins: v = “var”+str(i) for line in ins: exec(v+”=’str(line)'”) i+=1 another approach is to read the lines into a list: with open(“file_name.txt”, “r”) as … Read more

[Solved] C++ Chocolate Puzzle [closed]

Here is how I would solve this in Java: import java.util.HashMap; import java.util.Map; import java.util.Random; public class ChocolatePuzzle { private static final Map <String, Integer> solutions = new HashMap <String, Integer> (); private static final Map <String, Integer> bestMoves = new HashMap <String, Integer> (); private static int [] x; private static int k (int … Read more

[Solved] In plain C, without using strlen or any library function that uses strlen, how do you find whether one string is contained in another?

Removing the variable flag is deceptively easy by the way, as the only cases (using your algorithm as shown) where your function should return true is if either s1 is empty or the condition !*c is true. In all other cases it should return false. So your function, without any other modifications, could easily be … Read more

[Solved] Finding neighbours of an element in a matrix? [closed]

# 2dMatrix: Your matrix # x: x-coordinate of the element for which you’re searching for neighbors # y: y-coordinate of the element for which you’re searching for neighbors def nearest_elements_2dMatrix(2dMatrix, x,y): upleft = 2dMatrix[x-1][y-1] if (x-1 >= 0 and y-1>=0) else None downleft = 2dMatrix[x-1][y+1] if (x-1 >= 0 and y+1 < len(2dMatrix)) else None … Read more

[Solved] algorithm for slicing brute force keyspace [closed]

Let’s start with a slightly simpler problem. All keys are 8 numeric digits, and you have 10 machines. Simple enough – one machine checks 0???????, another checks 1??????? and so on. Note that this slicing doesn’t care that you’re doing multiprocessing – it’s just allocating ranges by fixing one of the digits. Your version is … Read more