Posts

Showing posts with the label algorithm

Closest Palindrome Number

Closest Palindrome Number I came across one of the common interview question which was to find the closest palindrome number. Say if the input is 127 then output will be 131 and if it is 125 then it should give 121 as output. I can come up with the logic but my logic fails on certain cases like 91, 911. In these inputs it give 99 , 919 but the correct output is 88 and 909. Algorithm steps are: Sometimes there's a tie: the closest palindromes to 1000 are 1001 and 999. I think it can be established that there is always one closest palindrome that has the same number of digits. However, as you noted, the closest palindrome may share no digits in common with the number. – Ted Hopp Sep 24 '13 at 17:50 Folding the string as you describe gives one of the closest palindromes, either upper ...

Difference between DFS vs BFS in this example?

Image
Difference between DFS vs BFS in this example? I am bit confused after reading examples at DFS where output is printed in different fashion for both DFS approaches though both are said to be DFS. In fact DFS recursion approach print the output just in same fashion as BFS. Then what's the difference ? Is recursion approach given here not an example of DFS ? Using Stack // Iterative DFS using stack public void dfsUsingStack(Node node) { Stack<Node> stack=new Stack<Node>(); stack.add(node); node.visited=true; while (!stack.isEmpty()) { Node element=stack.pop(); System.out.print(element.data + " "); List<Node> neighbours=element.getNeighbours(); for (int i = 0; i < neighbours.size(); i++) { Node n=neighbours.get(i); if(n!=null && !n.visited) { stack.add(n); n.visi...

Find most efficient groups of pairs

Image
Find most efficient groups of pairs I have a group of people, and I want each person to have a 1:1 meeting with every other person in the group. A given person can only meet with one other person at a time, so I want to do the following: To demonstrate the problem in terms of desired input/output, let's say I have the following list: >>> people = ['Dave', 'Mary', 'Susan', 'John'] I want to produce the following output: >>> for round in make_rounds(people): >>> print(round) [('Dave', 'Mary'), ('Susan', 'John')] [('Dave', 'Susan'), ('Mary', 'John')] [('Dave', 'John'), ('Mary', 'Susan')] If I had an odd number of people, then I would expect this result: >>> people = ['Dave', 'Mary', 'Susan'] >>> for round in make_rounds(people): >>> print(round) [('Dave', 'Mary...

How to determine if any value occurs more than twice in a list?

How to determine if any value occurs more than twice in a list? I have a list and would like to determine if any value occurs more than twice. I've tried using collections and counter but I cannot get those to evaluate to a single True or False value. myArray=[1,1,1,1,1,1,1,1,2] I would like it to return: True if any value occurs more than twice . True Any help is appreciated and it would really help if the solution was fast. I'm checking hundreds of thousands of lists. I'm new to programming and this is my first post. Where is your attempt? I see no code? – antfuentes87 7 mins ago 2 Answers 2 You could always construct a histogram of the values and see if any entry is greater than two. It could look something like this: def is_more_than_...

For an Algorithm, It is required to find the time analysis and a dynamic programming solution

For an Algorithm, It is required to find the time analysis and a dynamic programming solution Here is an algorithm in the attachment. It is required to find the time analysis of the algorithm and draw a recursion tree. Another way of doing this with Dynamic programming is required. An array of the result is also required. Questions Algorithm This is not a homework service. You have to demonstrate at least an attempted effort to solve your task. Once you have your version of dynamic programming solution - publish it here and somebody will review it. – dmitryro Jul 1 at 1:24 By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the webs...

Random generator without repeated result

Random generator without repeated result I need to put the numbers from low to high in an array randomly. low high For example given: low = 10, high = 15 a result like [ 12, 13, 10, 14, 11] is good. [ 12, 13, 10, 14, 11] This is a simple algorithm: iterate from low to high and try to fill in the empty slots on an array. const low = 1000 const high = 1010 const diff = high - low const arr = new Array(diff) for (var i = low; i < high; i++) { let success = false while(!success) { const index = Math.floor(Math.random() * diff) if (arr[index] === undefined) { arr[index] = i success = true } console.log(`${index} was ${success ? 'available' : 'taken'}`) } } console.log(arr) The problem is: in the end where most of the elements are filled, it is harder to find an unoccupied slot in the array. My question is: is there an algorithm that constantly generates unique new numbers until all the numbers are consumed? ...

Binary Search implementation via java

Binary Search implementation via java Please see the below java source code for binary search implementation public class Main { public static void main (String args) { int x= {1,2,3,4,5,6,7,8,9,10,11}; int y=binarySearch(x,11); System.out.println(y); }public static int binarySearch(int arr,int value) { int searchedIndex = -1; int first=0; **int last=arr.length-1;** int mid; while(first<=last) { mid=(first+last)/2; if(arr[mid]==value) { searchedIndex=mid; break; }else { if(value<arr[mid]) { last=mid-1; }else { first=mid+1; } } } return searchedIndex; } } int last=arr.length-1 is -1 compulsory or not.I feel that code works fine either last=arr.length-1 . If its compulsory please explain why. The array starts wi...

Improvement of performance for a maze solving program in Python

Image
Improvement of performance for a maze solving program in Python fellow programmers. I need help with one of my projects. I'm making a maze solving program. It reads an image file, which has to be black and white (black pixels are walls, white pixels are paths), have only one pixel at the top which is the entrance to the maze, and only one white pixel at the bottom which is the exit. There're three main parts to the code: 1) The program first create nodes in the maze, respecting a set of rules. For exemple here's a simple maze: and here're all the nodes drawn in red: The nodes are like the corners, the crossroads, every point where you can change direction. The distance between each node and the exit of the maze is also measured. While it's generating all the nodes, it places them in a list. 2) As soon as all the nodes are generated, the program will iterate through all the nodes in the list, and try to search in every direction possible for other nodes, to "lin...

how to write algorithm in pseudo-code [on hold]

how to write algorithm in pseudo-code [on hold] How to Write an algorithm in pseudo for finding the median (middle) value of a given array B. Assume: Array size =N Hint: Firstly, the array must be arranged is ascending or descending order If array size is odd, position of median value p=n+1/2 median B[p] If array size is even p =N/2 Median = (B[p]+B[p+1])/2 p=n+1/2 B[p] p =N/2 (B[p]+B[p+1])/2 Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question. Show what you've tried and what specifically you need help with. It isn't appropriate to dump homework here without an actual question. – Carcigenicate Jun 30 at 18:52 ...

How to create list of sets with k+1 elements from list of set with k elements if order does not matter?

How to create list of sets with k+1 elements from list of set with k elements if order does not matter? I am trying to implement a variant of the Apriori algorithm which involves forming lists of sets of size k+1 from lists of sets of size k. For example, if I had list [[1], [2], [3], [4]], I would like to form list [[1,2], [1,3], [1,4], [2,3], [2,4], [3,4]] and then[[1,2,3], [1,2,4], [2,3,4]]. I have considered using a LinkedHashSet data structure to prune out repeated elements, but LinkedHashSets don't prune out cases in structure [x, y] [y, x] which I want removed. Does anyone have any suggestions or experience in stuff like this? Thanks Do post the code which you have tried till now while asking questions on SO. – Pramod Jun 29 at 16:00 2 Answers 2 ...