How do I solve this: "Step forward and backward problem"? - python

The Problem Statement:
Sanjay is addicted to alcohol. Every night he drinks 4 bottles of vodka. He is going to his home. At first, he takes a step forward (which is 5m) but beacuse he is drunk, after his each step in forward direction, his body gets imbalanced and he takes a step backward (which is 3m).
Each step takes 1 min to complete. The distance from the bar to home is n metres. Calculate the time taken by him to reach his home.
Input Format:
single line containing one integer n.
Constraints:
0 <= n < 10^18
Output Format
single integer describing the time taken by him to reach home.
from math import *
n = int(input())
x = 0
m = 0
n = n % 1000000007
n = n % 1000000007
while x < n:
x += 5
m += 1
if x >= n:
break
x -= 3
m += 1
print(m)
But the time limit is exceeding in the last test case i.e. for n = 10^18 like numbers
Sample Input 0
11
Sample Output 0
7

The time taken is simply n/2 * 2
He advances 2 meters each "cycle" 5 forward 3 back
So we see how many "cycles" go into n (n / 2m) this will result
In the number of "cycles" taken to reach his house
Then we simply multiply by the amount of time taken per cycle (2 minutes)
to get the total time taken (t = n/2 * 2).

Try reducing the problem. Let time_taken(dist) be the function that tells us how long it takes to get home. Then the following hold:
time_taken(1) == 1
time_taken(2) == 1
time_taken(3) == 1
time_taken(4) == 1
time_taken(5) == 1
time_taken(6) == 1 * 2 + time_taken(4) (since 5-3 = 2)
== 1 * 2 + 1
time_taken(7) == 1 * 2 + time_taken(5)
== 1 * 2 + 1
time_taken(11) == 1 * 2 + time_taken(9)
== 2 * 2 + time_taken(7)
== 3 * 2 + time_taken(5)
== 3 * 2 + 1
time_taken(26) == 1 * 2 + time_taken(24)
== 2 * 2 + time_taken(22)
== ...
== 11 * 2 + time_taken(4)
== 11 * 2 + 1
if n > 5:
time_taken(n) == 1 * 2 + time_taken(n - 2)
== 2 * 2 + time_taken(n - 4)
== ...
== (formula here) * 2 + time_taken(4 or 5)

Related

why does my code multiply 2 times instead of one

My code should verify if a number is even, if it is, it prints the number multiplied by 2, if it isn't, it should print the number multiplied by 3, but just doesn't work.
m = int(input())
for i in range(m):
n = int(input())
n*=2 if n%2==0 else n*3
print(n)
When i try this input:
3
1
2
3
It returns:
3
4
**27** <- ?
n *= 2 if n % 2 == 0 else n * 3
means
n *= (2 if n % 2 == 0 else n * 3)
which means
if n % 2 == 0:
n = n * 2
else:
n = n * n * 3
You meant to write
n *= 2 if n % 2 == 0 else 3
n*=2 if n%2==0 else n*3
Operator precedence.
This statement is interpreted as
n *= (2 if n%2==0 else n*3)
And for input 3, n%2==0 is not true, so the statement becomes
n *= 9
Which is 27.

How can i calculate and print the sum of first N elements of below sequence using recursive function

How can i calculate and print the sum of first N elements of below sequence using recursive function
Example:
Sample Input N: 4
Sample Output: 7 + 12 + 17 + 22 = 58
I did some part of the code but it gives wrong result and I don't know where I'm making mistakes. That's why I need help!
def recur_sum(n):
if n <= 1:
return n
else:
for i in range(7,n+1):
return i + recur_sum(i-1)
num = int(input("Enter a number: "))
if num < 0:
print("Enter a positive number")
else:
print("The sum is",recur_sum(num))
As far as I understand your question, you want the sum of a sequence which each element of this sequence is increased by a step (such as 5), and it has an initial value (like 7), and you want the sum of first N (like 4) elements of this sequence.
At each recursion level, we add the current value to step , but when n == 1 we only return the current value (which is the Nth item of the sequence).
Do this:
def recur_sum(cur_val, step, n):
if n == 1:
return cur_val
return cur_val + recur_sum(cur_val+step,step,n-1)
num = int(input("Enter a number: "))
init_val = 7
step = 5
if num < 0:
print("Enter a positive number")
else:
print("The sum is",recur_sum(init_val, step, num))
Output:
The sum is 58
this returns a list of all sequential numbers starting at 7 each increment of 5. sum the return array.... change 5 and 2 to change increments for desired jumps/steps, and change the initial return value..
def recur_sum(n):
if n == 1:
return [7]
else:
return [5*n+2] + recur_sum(n-1)
num = int(input("Enter a number: "))
res = recur_sum(num)
print(sum(res))
A recursive function which returns all the elements up to n (where n is the input of your function) has already been proposed above.
In my understanding, you want a function with some recursive logic that return the sum of all the elements up to the n-th.
Your sequence is 7, 12, 17, 22, 27 and so forth. If we disect it:
it element sum sum is element is
1 7 7 1 * 7 + 0 * 5 1 * 7 + 0 * 5
2 12 19 2 * 7 + 1 * 5 1 * 7 + 1 * 5
3 17 36 3 * 7 + 3 * 5 1 * 7 + 2 * 5
4 22 58 4 * 7 + 6 * 5 1 * 7 + 3 * 5
5 27 85 5 * 7 + 10 * 5 1 * 7 + 4 * 5
If you want at any cost to implement a recursive solution, if is evident that at each step you need to increase the rolling sum by it * 7 + (it - 1) * 5 (where 7 is your start point, while 5 is your step).
You can implement a recursive solution as follows:
def recursive(n, step = 5, start = 7, counter = 1):
if n > 0:
this_element = start + (counter - 1) * step
if counter == n:
return this_element
else:
return this_element + recursive(n, step = step, start = start, counter = counter + 1)
else:
return 0
for i in range(1, 10):
print(recursive(i))
OUTPUT
7
19
36
58
85
117
154
196
243
From the table above you can see though that maybe a recursive solution is overkilling here given that the sum of elements up to n-th step has solution:
def my_sum(n, step = 5, start = 7):
return n * start + int(step * (n - 1) * n / 2)
for i in range(1, 10):
print(my_sum(i))
OUTPUT
7
19
36
58
85
117
154
196
243

A test interview question I could not figure out

So I wrote a piece of code in pycharm
to solve this problem:
pick any 5 positive integers that add up to 100
and by addition,subtraction or just using one of the five values
you should be able to make every number up to 100
for example
1,22,2,3,4
for 1 I could give in 1
for 2 i could give in 2
so on
for 21 I could give 22 - 1
for 25 I could give (22 + 2) - 1
li = [1, 1, 1, 1, 1]
lists_of_li_that_pass_T1 = []
while True:
if sum(li) == 100:
list_of_li_that_pass_T1.append(li)
if li[-1] != 100:
li[-1] += 1
else:
li[-1] = 1
if li[-2] != 100:
li[-2] += 1
else:
li[-2] = 1
if li[-3] != 100:
li[-3] += 1
else:
li[-3] = 1
if li[-4] != 100:
li[-4] += 1
else:
li[-4] = 1
if li[-5] != 100:
li[-5] += 1
else:
break
else:
if li[-1] != 100:
li[-1] += 1
else:
li[-1] = 1
if li[-2] != 100:
li[-2] += 1
else:
li[-2] = 1
if li[-3] != 100:
li[-3] += 1
else:
li[-3] = 1
if li[-4] != 100:
li[-4] += 1
else:
li[-4] = 1
if li[-5] != 100:
li[-5] += 1
else:
break
this should give me all the number combinations that add up to 100 out of the total 1*10 ** 10
but its not working please help me fix it so it prints all of the sets of integers
I also can't think of what I would do next to get the perfect sets that solve the problem
After #JohnY comments, I assume that the question is:
Find a set of 5 integers meeting the following requirements:
their sum is 100
any number in the [1, 100] range can be constructed using at most once the elements of the set and only additions and substractions
A brute force way is certainly possible, but proving that any number can be constructed that way would be tedious. But a divide and conquer strategy is possible: to construct all numbers up to n with a set of m numbers u0..., um-1, it is enough to build all numbers up to (n+2)/3 with u0..., um-2 and use um-1 = 2*n/3. Any number in the ((n+2)/3, um-1) range can be written as um-1-x with x in the [1, (n+2)/3] range, and any number in the (um-1, n] range as um-1+y with y in the same low range.
So we can use here u4 = 66 and find a way to build numbers up to 34 with 4 numbers.
Let us iterate: u3 = 24 and build numbers up to 12 with 3 numbers.
One more step u2 = 8 and build numbers up to 4 with 2 numbers.
Ok: u0 = 1 and u1 = 3 give immediately:
1 = u0
2 = 3 - 1 = u1 - u0
3 = u1
4 = 3 + 1 = u1 + u0
Done.
Mathematical disgression:
In fact u0 = 1 and u1 = 3 can build all numbers up to 4, so we can use u2 = 9 to build all numbers up to 9+4 = 13. We can prove easily that the sequence ui = 3i verifies sum(ui for i in [0, m-1]) = 1 + 3 + ... + 3m-1 = (3m - 1)/(3 - 1) = (um - 1) / 2.
So we could use u0=1, u1=3, u2=9, u3=27 to build all numbers up to 40, and finally set u4 = 60.
In fact, u0 and u1 can only be 1 and 3 and u2 can be 8 or 9. Then if u2 == 8, u3 can be in the [22, 25] range, and if u2 == 9, u3 can be in the [21, 27] range. The high limit is given by the 3i sequence, and the low limit is given by the requirement to build numbers up to 12 with 3 numbers, and up to 34 with 4 ones.
No code was used, but I think that way much quicker and less error prone. It is now possible to use Python to show that all numbers up to 100 can be constructed from one of those sets using the divide and conquer strategy.

Using set theory to find the shape area in Python

So I'm given the following diagram:
And I'm being asked to find the area of each polygon given n. The area is basically the sum of all blue squares since each square has an area of 1. So when n = 1, the area is one. When n = 2, the area is 5. Because of the relationship between each polygon, I know that I could knock this down using set theory.
n Area(n)
1 1
2 A(n-1) + (4 * (n-1)) = 5
3 A(n-1) + (4 * (n-1)) = 13
4 A(n-1) + (4 * (n-1)) = 25
5 A(n-1) + (4 * (n-1)) = 41
However, I didn't have as much luck trying to represent this in code:
def shapeArea(n):
prev_output = 0
output = 0
if n == 1:
output = 1
elif n > 1:
for i in range(n):
prev_output = n-1 + (4 * (n-1))
output = prev_output + (4 * (n-1))
return output
For example: For n = 2, I'm getting an output of 9 instead of 5.
You were close :-)
Here are the small fix-ups:
def shapeArea(n):
output = 1
for i in range(1, n):
output += 4 * i
return output
Running this:
for n in range(1, 6):
print(n, shapeArea(n))
Gives this output:
1 1
2 5
3 13
4 25
5 41
of course with Gauss's theorem the code would look like this:
def ShapeArea(n):
return 2*(n**2) - (2*n)+1
In a recursive solution, it is much simple from your logic.
def shape_area(n):
if n == 1:
return 1
return shape_area(n-1) + 4*(n-1)
You can use Gauss' trick to make a closed-form formula:
2*Area(n) =
1 + 4 + 8 + 12 + ... + 4(n-1)
+
1 + 4(n-1) + 4(n-2) + 4(n-3) + ... + 4
--------------------------------------------------
2 + 4n + 4n + 4n + ... + 4n
= (n-1)4n + 2
= 4n^2 - 4n + 2
So Area(n) = 2n2 - 2n + 1
def shape_area(n):
return sum([1] + [4*i for i in range(1, n)])

All + - / x combos for 4 numbers

4 one digit numbers and try to get all the possible combos
4511
like 4 + 5 + 1 x 1
Code to get first, 2nd 3rd and 4th numbers
numbers = input("Input 4 numbers separated with , : ")
numlist = numbers.split(",")
print (numlist)
No1 = int(numlist.pop(0))
No2 = int(numlist[0])
No3 = int(numlist[1])
No4 = int(numlist[2])
An exhaustive search:
from random import randrange
from itertools import permutations
def solve(digits):
for xs in permutations(digits):
for ops in permutations('+-*/', 3):
equation = reduce(lambda r, (x, op): '{0} {1} {2}'.format(r, op, float(x)), zip(xs[1:], ops), xs[0])
try:
if eval(equation) == 10:
yield equation.replace('.0','')
except ZeroDivisionError:
pass
digits = [randrange(0,10,1) for x in range(4)]
solutions = list(solve(digits))
if solutions:
print '\n'.join(solutions)
else:
print 'No solution for {0}'.format(digits)
Example output:
2 * 5 / 1 + 0
2 * 5 / 1 - 0
2 * 5 + 0 / 1
2 * 5 - 0 / 1
2 / 1 * 5 + 0
2 / 1 * 5 - 0
5 * 2 / 1 + 0
5 * 2 / 1 - 0
5 * 2 + 0 / 1
5 * 2 - 0 / 1
5 / 1 * 2 + 0
5 / 1 * 2 - 0
0 + 2 * 5 / 1
0 + 2 / 1 * 5
0 + 5 * 2 / 1
0 + 5 / 1 * 2
0 / 1 + 2 * 5
0 / 1 + 5 * 2
or
No solution for [7, 1, 0, 2]
Note: I like the recursive expression generator referred to in the comment above, but I just don't think recursively straight off.
Use a recursive approach:
numbers = input("Input 4 numbers separated with , : ")
numlist = list(numbers)
def f(lst,carry,result):
x = lst.pop(0)
if lst == []:
return carry+x == result or \
carry-x == result or \
carry*x == result or \
carry/x == result
elif carry==None:
carry = x
return f(lst,carry,result)
else:
return f(lst,carry+x,result) or\
f(lst,carry-x,result) or\
f(lst,carry*x,result) or\
f(lst,carry/x,result)
print(f(numlist, None, 10))
Your I/O is wrong (first two lines), and I don't know if this is something that you have already tried.

Categories