You should!! map(),filter(), reduce() and zip() are really useful.
Btw, I have only used it for taking inputs in single line and type cast string to integer i.e. same as “cin >> a >> b >> c” in C++
Problem 9:
You are given an array of nonnegative integers. Let’s say you start at the beginning of the array and are trying to advance to the end. You can advance at most, the number of steps that you’re currently on. Determine whether you can get to the end of the array.
For example, given the array [1, 3, 1, 2, 0, 1] , we can go from indices 0 -> 1 -> 3 -> 5 , so return true .
Given the array [1, 2, 1, 0, 0] , we can’t reach the end, so return false .
I wrote a solution but I’m not 100% sure I understood the problem (bad practice I know :p)
what I understood:
we start from the first index, and the next index we jump to is currentindex+the value in that current index.
we keep doing this until either:
we get to the end
we go out of bound
the value we get to is zero (we can’t go forward anymore).
Based on that explanation, here is the function that does the job, in c++:
for Problem 9:
Each state defines number of jumps can be made from one node to other.
for example
state 5 , represent jumps at max 5 and at min 0 and
to solve you must reach the end of the node.
Assumption
All states are whole number.
edge cases
If the current node is the last node.
if all the nodes state are 0.
if node is not end_indx and value is 0.
Pseudocode for top down approach:
func jump_resolver(arr,curr_indx)
if curr_indx is end_indx (reached end )
return true;
end if
if curr_indx past end_indx (greater than end_indx)
return false
end if
state_val = arr[curr_indx]
if state_val == 0 // no increment we stop here
return false;
for i=state_val to 1 do
reached = jump_resolver(arr,curr_indx+i);
if reached
return true;
end for
return false; // as we tried all the jumps
Pseudocode for bottom up approach:
a. using stack iterativerly
stack initalized with {fnstate:‘CALL’,ctxt:{indx:0}
dest = false // is end reached
while stack.hasElements()
fnstate,ctxt= stk.pop() // here pop and get what is the current function state and context
if fnstate == 'CALL'
if curr_indx past end_indx (greater than end_indx)
dest = false
continue; // moves to start of while
end if
if ctxt.indx == end_indx
dest = true;
else // inner else
val = arr[ctxt.indx]; // fetch the value from current index of arr
if val == 0
dest = false // if it's not nth index and it's value is 0 , return false
continue; // move forward with while loop
end if
push to stk {fnstate:'RES',ctxt:{indx:ctxt.indx} // later we will resume at context index
for i = val to 1 do
push to stk {fnstate:'CALL',ctxt:{indx:ctxt.indx+i}
if dest // on destination reach
break;
end for // end inner for
end while // outer while
Note
There is even better way to do this via disjoint set.
hello guys
i have a quick simple question , i want to code a food app that have an integrated delivery tracking system and chat
what am i going to need ?
theory
fetch string , convert to array of strings
assign each string a value based on number of vowels
sort it and display.
pseudocode
func display()
vals[] // array of vals after split(" ")
use treeMap (modify how elements are
compared , here elements are string with help of spit_vowl_count)
as you keep adding str to tree , it sorts and adds the element
for val to treeMap
console val + ' '
end for // you can use iterator pattern if you want
end func
func spit_vowl_count(str)
sum = 0
for i 0 to len(str)
if str(i) belongs to `a` , `e`, `i`, `o`, `u`
sum + = 1;
end for
return sum
end func
//function to count vowel of each item
const countVowels = (s)=> {
let vowels = 'aeiouAEIOU';
let count = 0;
for(let i = 0; i < s.length ; i++)
if (vowels.indexOf(s[i]) !== -1)
count += 1;
return count;
}
//function to sort accoring to the no of vowels
const vowelSort = (str) => {
let s = str.split(' ').sort((a,b)=>{
let countA = countVowels(a);
let countB = countVowels(b);
if (countA < countB)
return -1;
if (countA > countB)
return 1;
})
return s.join(' '); //returning the final output
}
vowelSort('java is good but javascript is awesome');
Write a function persistence that takes in a positive parameter num and returns its multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a single digit.
For example:
persistence(39) === 3 // because 39 = 27, 27 = 14, 14= 4 and 4 has only one digit
persistence(999) === 4 // because 999 = 729, 729 = 126, 126 = 12, and finally 12 = 2
persistence(4) === 0 // because 4 is already a one-digit number