pseudocode

Algorithm for 'good' number

回眸只為那壹抹淺笑 提交于 2019-12-02 12:58:33
A give number x is 'good' if the sum of any two consecutive digit of the number x are between k and 2k. I need to find an algorithm that for a given number k and a given number n, find how many 'good' n-digit numbers exist. I made an implementation for this in PHP, but the complexity is to big (i am searching for all those 'good' number and counting them, so the complexity is O(10^n)). <?php $n = 5; $k = 5; $min = $k*1; $max = $k*2; $counter = 0; for ($i = pow(10, $n-1); $i<pow(10,$n); $i++) { $number = $i; $prev = $number % 10; $number = $number / 10; while($number >= 10) { $crnt = $number %

Translating a mips pseudo instruction 'rol'

橙三吉。 提交于 2019-12-02 11:57:30
I'm trying to translate the mips pseudo instruction rol (rotate left). I need to translate it before I can submit an assignment, which is unfortunate because the pseudo instruction worked quite well for me. The instruction was, rol $s4,$s4, 1 #goes to the left most bit (which as it says, gets the left most bit) the way I translated it was, lui $s4, 0x8001 ori $t0, $s4, 0x0004 srl $s4, $t0, 31 sll $s5, $t0, 1 or $s5, $s5, $s3 but it completely messes up my code, can someone please help me translate it correctly? Or tell me what I'm doing wrong? Thank you in advance, Here is a UT-Dallas lecture

PHP time intervals

走远了吗. 提交于 2019-12-02 09:37:00
I'm looking for a solution to something which seems like it should be pretty simple, but it doesn't seem that I can find any good answers on here, and I can't seem to get it to work myself. What I'm looking for is to set a start time, an end time, and then iterate through a set of times between given a time interval. Say, for example, 9:00 AM - 5:00 PM are the start times, and the values returned between these times at half hour intervals are 9:30, 10:00, 10:30, 11:00, etc. Here's some pseudo-code, because I'm not exactly sure what I need to plug in here (at least nothing I've tried has worked

AVL Tree: Finding the key with the smallest data values in keys between two values in O(logn) time

会有一股神秘感。 提交于 2019-12-02 08:51:46
So I'm given an AVL tree. And im trying to figure out at least the pseudocode to find the key with the minimum data values among all of the keys between two values k1 and k2. This is assuming that the field data stored in each node is an integer. I want to make sure that my pseudocode runs in O(logn) time. I know that I can do it by storing an extra field in the node structure..and showing how this field can be maintained during updates, but I don't know where to go from there. Let's assume that the node structure looks like this (Java). class Node { Node left; Node right; int key; int value;

Nested loops result

倖福魔咒の 提交于 2019-12-02 07:05:29
I really don't know how to find out the result of nested loops. For example in the following pseudo-code, I can't sort out what will be given at the end of execution. I'll be so glad if anyone gives me a simple solution. r <- 0 for i <- 1 to n do for j <- 1 to i do for k <- j to i+j do r <- r + 1 return r Question is: What is the result of the code and give the result r in terms of n ? I write it but every time I get confused. Grijesh Chauhan In your pseudo-code, Inner most loop, k <- j to i+j can be written as k <- 0 to i ( this is by removing j ). Hence your code can be simplified as follows

Time complexity analysis. choosing operator for counting number of times a line of code runs

自古美人都是妖i 提交于 2019-12-02 05:22:45
Analysing time complexity of this pseudocode. On the right my take on the number of times each line runs. I'm not sure whether to use log n , n log n , or simply n for the while-loop..please help times 1 sum = 0 1 2 i = 1 1 3 while i ≤ n log n + 1 4 for j = 1 to n n log n 5 sum = sum + j n log n 6 i = 2i log n 7 return sum 1 this results in: 2 n log + 2log n + 4 and thereby : O(n log n) is this correct ? If your while loop is: 3 while i < n log n + 1 4 for j = 1 to n n log n 5 sum = sum + j n log n 6 i = 2i log n Then yes, you are correct in calculating the complexity. The complexity of the

Tarjan's Algorithm: Time Complexity and slight modification possibility

戏子无情 提交于 2019-12-02 04:11:49
This question is related to but not the same as the one recently asked here . I just read the Wikipedia psuedocode . algorithm tarjan is input: graph G = (V, E) output: set of strongly connected components (sets of vertices) index := 0 S := empty for each v in V do if (v.index is undefined) then strongconnect(v) end if end for function strongconnect(v) // Set the depth index for v to the smallest unused index v.index := index v.lowlink := index index := index + 1 S.push(v) // Consider successors of v for each (v, w) in E do if (w.index is undefined) then // Successor w has not yet been visited

How to make a pipe in c++

孤街浪徒 提交于 2019-12-01 17:49:44
I'm looking at the code for a c++ program which pipes the contents of a file to more. I don't quite understand it, so I was wondering if someone could write pseudocode for a c++ program that pipes something to something else? Why is it necessary to use fork? create pipe fork process if child: connect pipe to stdin exec more write to pipe You need fork() so that you can replace stdin of the child before calling, and so that you don't wait for the process before continuing. You will find your answer precisely here Why is it necessary to use fork? When you run a pipeline from the shell, eg. $ ls

How to make a pipe in c++

风流意气都作罢 提交于 2019-12-01 17:39:38
问题 I'm looking at the code for a c++ program which pipes the contents of a file to more. I don't quite understand it, so I was wondering if someone could write pseudocode for a c++ program that pipes something to something else? Why is it necessary to use fork? 回答1: create pipe fork process if child: connect pipe to stdin exec more write to pipe You need fork() so that you can replace stdin of the child before calling, and so that you don't wait for the process before continuing. 回答2: You will

Algorithm for computing partial orderings of dependency graphs

霸气de小男生 提交于 2019-12-01 15:48:54
I'm trying to compute a partial "topological sort" of a dependency graph, which is actually a DAG (Directed Acyclic Graph) to be precise; so as to execute tasks without conflicting dependencies in parallel. I came up with this simple algorithm because what I found on Google wasn't all that helpful (I keep finding only algorithms that themselves run in parallel to compute a normal topological sort). visit(node) { maxdist = 0; foreach (e : in_edge(node)) { maxdist = max(maxdist, 1 + visit(source(e))) } distance[node] = maxdist; return distance[node]; } make_partial_ordering(node) { set all node