I need to use the formula v = (start * 2) + 5 to print the resulting values. The start parameter is the value in which the sequence starts and the count parameter is how many values get printed. I have no idea how to get the base case to get called.
def print_sequence_rec(start, count):
else:
v = (start * 2) + 5
print(v, end="")
print_sequence_rec(start, count + 1)
For example, the function print_sequence_rec(2, 5) should print 2 9 23 51 107 219
Make that count - 1 instead of + 1 so that count decreases by one each call. The base case is when count == 0.
You'll also need to do something with v. I suggest passing it to the recursive invocation.
def print_sequence_rec(start, count):
# base case
if count == 0:
pass
# recursive case
else:
v = (start * 2) + 5
print(v, end="")
print_sequence_rec(v, count - 1)
Or more likely you should get rid of it and move the computation to the recursive call:
def print_sequence_rec(start, count):
# base case
if count == 0:
pass
# recursive case
else:
print(start, end="")
print_sequence_rec((start * 2) + 5, count - 1)
John's answer does a nice job addressing your question, but this actually seems like a good case to use a generator.
For example...
def gen_sequence_rec(start, count):
while count >= 0:
yield start
start = (start * 2) + 5
count -= 1
...then you can save the sequence as a list...
seq = list(gen_sequence_rec(2,5))
#[2, 9, 23, 51, 107, 219]
...and print however you want...
print(seq)
[2, 9, 23, 51, 107, 219]
print(' '.join([str(x) for x in seq]))
2 9 23 51 107 219
Related
I wonder if there is any function in python like that.
Example 01:
For example, if I specify the range from (1,12) integer. Then:
if current value is 12 and I add 1, it return 1 (go back to minimum)
if current value is 11 and I add 3, it return 2
Example 02:
Another example is if I set range from (5, 9). Then:
If current value is 8 and I add 2, it return 5
If current value is 8 and I add 7, it return 5
I know I can write a self-defined function for that, I am just curious to know if python has that similar function built in
The self-defined function:
def wrap_range(val, nmin, nmax, add_val):
nrange = nmax - nmin + 1
remain = add_val % nrange
val = val + remain
if val <= nmax:
return val
else:
val = val - nmax + nmin - 1
return val
Itertools has the cycle and islice functions that you can use to get what you want -
from itertools import cycle, islice
def repeated_offset(nmin, nmax, start, n):
rng = range(nmin, nmax + 1)
start_idx = list(rng).index(start)
value, *_ = islice(cycle(rng), start_idx + n, start_idx + n + 1)
return value
repeated_offset(1, 12, 12, 1)
# 1
repeated_offset(1, 12, 11, 3)
# 2
repeated_offset(5, 9, 8, 2)
# 5
repeated_offset(5, 9, 8, 7)
# 5
What about looping, subtracting while the total value is bigger than the lower boundary, while subtracting range wide.
def loopit(val, the_min, the_max, addition):
total = val + addition
diff = the_max - the_min + 1
if not total > the_min:
raise ValueError("The total value must be larger then lowest boundary of the range")
while the_max < total:
total -= diff
return total
if __name__ == '__main__':
print(loopit(12, 1, 12, 1))
print(loopit(11, 1, 12, 3))
print(loopit(8, 5, 9, 2))
print(loopit(8, 5, 9, 7))
output:
1
2
5
5
I want to find the number of ways, a given integer X can be decomposed into sums of numbers which are N-th powers and every summand must be unique. For example if X = 10 and N=3, I can decompose this number like that:
10 = 2^3+1^3+1^3 ,but this is not a valid decomposition, because the number 1 appears twice. A valid decomposition for X = 10 and N = 2 would be 10 = 3^2+1^2, since no summand is repeating here.
Now I tried it to use recursion and created the following Python Code
st = set(range(1,int(pow(X,1/float(N))))) # generate set of unique numbers
print(str(ps(X, N, st)))
def ps(x, n, s):
res = 0
for c in s:
chk = x-pow(c,n) # test validity
if chk > 0:
ns = s-set([c])
res += ps(chk,n,ns)
elif chk == 0:
res += 1 # one result is found
else:
res += 0 # no valid result
return res
I used a set called st and then I recursively called the function ps that includes the base case "decomposition found" and "decomposition not found". Moreover it reduces a larger number to a smaller one by considering only the ways how to decompose a given number into only two summands.
Unfortunately, I get completely wrong results, e.g.
X = 100, N = 3: Outputs 0, Expected 1
X = 100, N = 2: Outputs 122, Expected 3
X = 10, N = 2: Outputs 0, Expected 1
My thoughts are correct, but I think the Problem is anywhere in the recursion. Does anybody see what I make wrong? Any help would be greatly appreciated.
Hint:
>>> X = 100
>>> N = 3
>>> int(pow(X, 1/float(N)))
4
>>> list(range(1, 4))
[1, 2, 3]
The output is indeed correct for the input you are feeding it.
The problem is line res += 1 # one result is found in conjuction with res += ps(chk,n,ns) will make the algorithm add twice.
E.g X = 10, N = 2: Outputs 0, Expected 1 because:
c=1:
10 - 1^2 > 0 -> res += ps(chk,n,ns)
c=3:
9 - 3^2 == 0 -> res += 1 # one result is found ... return res
So, in c=3 res=1 is returned to the c=1 call, which will
res += ps(chk,n,ns), and ps(chk,n,ns) = 1, making res = 2 and doubling the result expected.
E.g. X = 29, N = 2.
29 = 2^2 + 3^2 + 4^2
Solving from bottom to top (the algorithm flow):
c=4 -> res += 1... return res
c=3 -> res += ps() -> res += 1 -> res = 2 ... return res
c=2 -> res += ps() -> res += 2 -> res = 4 ... return res
But res is supposed to be 1.
Solution: You cannot add res to res. And you must remove the previous iterated objects to avoid path repetition. Check the solution below (with prints for better understanding):
def ps(x, n, s):
print(s)
print("")
return ps_aux(x, n, s, 0) # level
def ps_aux(x, n, s, level):
sum = 0
for idx, c in enumerate(s):
print("----> " * level + "C = {}".format(c))
chk = x - pow(c,n) # test validity
if chk > 0:
ns = s[idx + 1:]
sum += ps_aux(chk,n,ns, level + 1)
elif chk == 0:
print("OK!")
sum += 1 # one result is found
else:
sum += 0 # no valid result
return sum
Try with:
X=10 # 1 solution
N=2
st = list(range(1,int(pow(X,1/float(N))) + 1 )) # generate set of unique numbers
print(str(ps(X, N, st)))
X=25 # 2 solutions [3,4], [5]
N=2
st = list(range(1,int(pow(X,1/float(N))) + 1 )) # generate set of unique numbers
print(str(ps(X, N, st)))
I need a function that takes n and returns 2n - 1 . It sounds simple enough, but the function has to be recursive. So far I have just 2n:
def required_steps(n):
if n == 0:
return 1
return 2 * req_steps(n-1)
The exercise states: "You can assume that the parameter n is always a positive integer and greater than 0"
2**n -1 is also 1+2+4+...+2n-1 which can made into a single recursive function (without the second one to subtract 1 from the power of 2).
Hint: 1+2*(1+2*(...))
Solution below, don't look if you want to try the hint first.
This works if n is guaranteed to be greater than zero (as was actually promised in the problem statement):
def required_steps(n):
if n == 1: # changed because we need one less going down
return 1
return 1 + 2 * required_steps(n-1)
A more robust version would handle zero and negative values too:
def required_steps(n):
if n < 0:
raise ValueError("n must be non-negative")
if n == 0:
return 0
return 1 + 2 * required_steps(n-1)
(Adding a check for non-integers is left as an exercise.)
To solve a problem with a recursive approach you would have to find out how you can define the function with a given input in terms of the same function with a different input. In this case, since f(n) = 2 * f(n - 1) + 1, you can do:
def required_steps(n):
return n and 2 * required_steps(n - 1) + 1
so that:
for i in range(5):
print(required_steps(i))
outputs:
0
1
3
7
15
You can extract the really recursive part to another function
def f(n):
return required_steps(n) - 1
Or you can set a flag and define just when to subtract
def required_steps(n, sub=True):
if n == 0: return 1
return 2 * required_steps(n-1, False) - sub
>>> print(required_steps(10))
1023
Using an additional parameter for the result, r -
def required_steps (n = 0, r = 1):
if n == 0:
return r - 1
else:
return required_steps(n - 1, r * 2)
for x in range(6):
print(f"f({x}) = {required_steps(x)}")
# f(0) = 0
# f(1) = 1
# f(2) = 3
# f(3) = 7
# f(4) = 15
# f(5) = 31
You can also write it using bitwise left shift, << -
def required_steps (n = 0, r = 1):
if n == 0:
return r - 1
else:
return required_steps(n - 1, r << 1)
The output is the same
Have a placeholder to remember original value of n and then for the very first step i.e. n == N, return 2^n-1
n = 10
# constant to hold initial value of n
N = n
def required_steps(n, N):
if n == 0:
return 1
elif n == N:
return 2 * required_steps(n-1, N) - 1
return 2 * required_steps(n-1, N)
required_steps(n, N)
One way to get the offset of "-1" is to apply it in the return from the first function call using an argument with a default value, then explicitly set the offset argument to zero during the recursive calls.
def required_steps(n, offset = -1):
if n == 0:
return 1
return offset + 2 * required_steps(n-1,0)
On top of all the awesome answers given earlier, below will show its implementation with inner functions.
def outer(n):
k=n
def p(n):
if n==1:
return 2
if n==k:
return 2*p(n-1)-1
return 2*p(n-1)
return p(n)
n=5
print(outer(n))
Basically, it is assigning a global value of n to k and recursing through it with appropriate comparisons.
I am trying to perform a binary search on a list in python. List is created using command line arguments. User inputs the number he wants to look for in the array and he is returned the index of the element. For some reason, the program only outputs 1 and None. Code is below.
Any help is extremely appreciated.
import sys
def search(list, target):
min = 0
max = len(list)-1
avg = (min+max)/2
while (min < max):
if (list[avg] == target):
return avg
elif (list[avg] < target):
return search(list[avg+1:], target)
else:
return search(list[:avg-1], target)
print "The location of the number in the array is", avg
# The command line argument will create a list of strings
# This list cannot be used for numeric comparisions
# This list has to be converted into a list of ints
def main():
number = input("Please enter a number you want to search in the array !")
index = int(number)
list = []
for x in sys.argv[1:]:
list.append(int(x))
print "The list to search from", list
print(search(list, index))
if __name__ == '__main__':
main()
CL :
Anuvrats-MacBook-Air:Python anuvrattiku$ python binary_search.py 1 3 4 6 8 9 12 14 16 17 27 33 45 51 53 63 69 70
Please enter a number you want to search in the array !69
The list to search from [1, 3, 4, 6, 8, 9, 12, 14, 16, 17, 27, 33, 45, 51, 53, 63, 69, 70]
0
Anuvrats-MacBook-Air:Python anuvrattiku$
In Python2 and Python3 you can use bisect as written in the comments.
Replace your search with the following
from bisect import bisect_left
def search(alist, item):
'Locate the leftmost value exactly equal to item'
i = bisect_left(alist, item)
if i != len(alist) and alist[i] == item:
return i
raise ValueError
alist = [1,2,7,8,234,5,9,45,65,34,23,12]
x = 5
alist.sort() # bisect only works on sorted lists
print(search(alist, x)) # prints 2 as 5 is on position 2 in the sorted list
Also, the AS SortedCollection (Python recipe) could be useful.
The following code (from here) performs the binary search and returns position and if the item was found at all.
def binarySearch(alist, item):
first = 0
last = len(alist)-1
found = False
while first<=last and not found:
pos = 0
midpoint = (first + last)//2
if alist[midpoint] == item:
pos = midpoint
found = True
else:
if item < alist[midpoint]:
last = midpoint-1
else:
first = midpoint+1
return (pos, found)
Will return (2, True) if used in the example above.
Well, there are some little mistakes in your code. To find them, you should either use a debugger, or at least add traces to understand what happens. Here is your original code with traces that make the problems self evident:
def search(list, target):
min = 0
max = len(list)-1
avg = (min+max)/2
print list, target, avg
...
You can immediately see that:
you search in a sub array that skips avg-1 when you are below avg
as you search in a sub array you will get the index in that subarray
The fixes are now trivial:
elif (list[avg] < target):
return avg + 1 + search(list[avg+1:], target) # add the offset
else:
return search(list[:avg], target) # sublist ends below the upper limit
That's not all, when you end the loop with min == max, you do not return anything (meaning you return None). And last but not least never use a name from the standard Python library for your own variables.
So here is the fixed code:
def search(lst, target):
min = 0
max = len(lst)-1
avg = (min+max)/2
# uncomment next line for traces
# print lst, target, avg
while (min < max):
if (lst[avg] == target):
return avg
elif (lst[avg] < target):
return avg + 1 + search(lst[avg+1:], target)
else:
return search(lst[:avg], target)
# avg may be a partial offset so no need to print it here
# print "The location of the number in the array is", avg
return avg
Recursive:
def in_list(l, x):
if len(l) < 2:
if l[0] == x:
return True
else:
return False
mid = len(l) // 2
if x < l[mid]:
return in_list(l[:mid], x)
else:
return in_list(l[mid:], x)
or iterative:
def in_list2(l, x):
low = 0
high = len(l) - 1
while low <= high:
mid = (low + high) // 2
if l[mid] == x:
return True
if x < l[mid]:
high = mid - 1
else:
low = mid + 1
return False
#Serge Ballesta 's solution is undoubtly the correct answer to this question.
I am just going to add another way of solving this:
def search(arr, item, start, end):
if end-start == 1:
if arr[start] == item:
return start
else:
return -1;
halfWay = int( (end-start) / 2)
if arr[start+halfWay] > item:
return search(arr, item, start, end-halfWay)
else:
return search(arr, item, start+halfWay, end)
def binarysearch(arr, item):
return search(arr, item, 0, len(arr))
arr = [1, 3, 4, 6, 8, 9, 12, 14, 16, 17, 27, 33, 45, 51, 53, 63, 69, 70]
print("Index of 69: " + str(binarysearch(arr, 69))) # Outputs: 16
The reason you aren't getting correct result is because in every recursive call your code is sending sliced array. So the array length keeps reducing. Ideally you should work out a way to send original array and work with only start, end indices.
Over the weekend I was working on the Ad Infinitum challenge on HackerRank.
One problem was to calculate the sum of all subsequences of a finite sequence, if each subsequence is thought of as an integer.
For example, sequence 4,5,6 would give answer 4 + 5 + 6 + 45 + 46 + 56 + 456 = 618.
I found a recursion and wrote the Python code below. It solved 5/13 test cases.
The remaining 8/13 test cases had runtime errors.
I was hoping someone could spy where in the code the inefficiencies lie, and how they can be sped up. Or, help me decide that it must be that my recursion is not the best strategy.
# Input is a list, representing the given sequence, e.g. L = [4,5,6]
def T(L):
limit = 10**9 + 7 # answer is returned modulo 10**9 + 7
N = len(L)
if N == 1:
return L[0]
else:
last = L[-1]
K = L[:N-1]
ans = T(K)%limit + 10*T(K)%limit + (last%limit)*pow(2,N-1,limit)
return ans%limit
This is my submission for the same problem (Manasa and Sub-sequences).
https://www.hackerrank.com/contests/infinitum-may14/challenges/manasa-and-sub-sequences
I hope this will help you to think of a better way.
ans = 0
count = 0
for item in raw_input():
temp = (ans * 10 + (count + 1)*(int(item)))%1000000007
ans = (ans + temp)%1000000007
count = (count*2 + 1)%1000000007
print ans
Well, you want the combinations:
from itertools import combinations
def all_combinations(iterable):
for r in range(len(digits)):
yield from combinations(digits, r+1)
And you want to convert them to integers:
def digits_to_int(digits):
return sum(10**i * digit for i, digit in enumerate(reversed(digits)))
And you want to sum them:
sum(map(digits_to_int, all_combinations([4, 5, 6])))
Then focus on speed.
Assuming you mean continuous subsequence.
test = [4, 5, 6]
def coms(ilist):
olist = []
ilist_len = len(ilist)
for win_size in range(ilist_len, 0, -1):
for offset in range((ilist_len - win_size) + 1):
subslice = ilist[offset: offset + win_size]
sublist = [value * (10 ** power) for (power, value) in enumerate(reversed(subslice))]
olist.extend(sublist)
return olist
print sum(coms(test))