How do these simple Python functions work? [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have run the the following functions and studied them line-by-line. I understand how the outer for-loop in f(n) works and how the while-loop works in g(n) but I understand the role of the inner for-loop in f(n). Also, how do these loops work with t = t+1? Thanks in advance!
def f(n):
t=0
for i in range(n):
for j in range(2*i):
t=t+1
return t
f(5)
def g(n):
t=0
j=n
while j>1:
t = t+1
j = j/2
return t
g(32)

The inner loop keeps adding 1 to t until it adds 2 times each item from the outer loop. So it adds 0 + 2 + 4 + 6 + 8. The range(5) is similar to a list equivalent to [0,1,2,3,4].

t=t+1 simply adds 1 to the value of t every time the line is run.

Related

Reduce steps in this simple maze puzzle? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
Video Puzzle
Current code:
for i in range(6,0,-2):
Spaceship.step(2)
Dev.step(i)
for idk in range(3):
Dev.turnRight()
Dev.step(i*2)
Dev.turnRight()
Dev.step(i)
In this puzzle the objective is to get all the item (blue thing). With 6 line of code, and I'm currently at 8 line of code. I don't know how to minimalize the line of code.
Note:
Dev.step() is the robot, it can go backward by set the value by negative.
Spaceship.step() is the spaceship, it can not go backward.
You can avoid pythonesque code like so:
for i in range(6,0,-2):
Spaceship.step(2)
for idk in range(4):
Dev.step(i)
Dev.turnRight()
Dev.step(i)
This is a possible solution in only 6 lines:
for i in range(6, 0, -2):
Spaceship.step(2)
for k, j in enumerate([1, 2, 2, 2, 1]):
Dev.step(i * j)
if k != 4:
Dev.turnRight()
The idea is to group all steps of the robot in a list in order to do a nested loop and turn only if it is not the last element of the list.

Python. How to sum each element with multi times in loop? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
Example:
import numpy
a = [1,2,3,4,5]
b = []
for i in range(len(a)):
b.append(a[i]+1)
Here, I have b = [2,3,4,5,6].
But I want sum multi times...(maybe I want to sum with 3 times). I expected after three times sum I have b = [4,5,6,7,8].
So, how I can get b=[4,5,6,7,8] from a=[1,2,3,4,5] with 3 times add 1 with loop?
Add 3 to each element using a list comprehension;
b = [i+3 for i in a]
or as a function, where you can change the value added to the list easily;
def add_k_to_list(k, a_list):
return [i+k for i in a_list]
To repeatedly apply as per your comment;
def add_k_to_list(k, a_list):
for _ in range(k):
a_list = [i+1 for i in a_list]
return(a_list)

check to see if elements in one list occurs in another list with recursions [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
firstLst = [1,2,3,4]
secondLst = [5,6,3,2,4,1]
If i am passing the two lists above as arguements in a function. I would like to know how I can check if all elements in the firstLst are in the secondLst using recursions with Python. I can achieve this by having two extra parameters i and j which will allow me to check each element in firstLst and secondLst, but is there a way to do this by only having the two lists as parameters?
example of the function with parameters:
def firstInSecond(lst1,lst2):
How can True be returned if all elements in lst1 are in lst2 otherwise False is returned. I hope this makes sense.
This isn't exactly recursive, but simpler.
set(firstLst).issubset(set(secondLst))
For a recursive answer,
def firstInSecond(lst1,lst2):
if len(lst1) == 0:
return True
if lst1[0] in lst2:
return firstInSecond(lst1[1:], lst2)
else:
return False

Convert a List to integer without using map() or join() [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
if I have
list=[1,2,3,4,5,6]
How can i make it
list=123456
Thanks for your help in advance!
You can do this:
inlist=[1,2,3,4,5,6]
length = len(inlist)
s = 0
for i in range(length):
s += (inlist[i] * ( 10 ** (length-1-i)))
inlist = s
print(inlist)
This will give you:
123456
You need to utilize the power of 10 to multiply it with each number.
Note that you shouldn't use list as a variable name as it is a Python keyword.
Another version (without using any built-in functions at all):
inlist=[1,2,3,4,5,6]
count = 1
s = 0
for elem in inlist[::-1]:
s += (elem * ( 10 ** (count-1)))
count += 1
inlist = s
print(inlist)
you can do it by for and join likes the following:
int(''.join([str(i) for i in my_list]))

sumDivs could you help me with my code please [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Write a function called sumDivs that accepts a single, positive integer and calculates the
sum of all of its proper divisors. Use a for loop to find and sum the proper divisors of the
given integer.
For example,
>>> sumDivs(8)
7
this is what i have:
def sums(n):
i=0
for i in range (1, n-1):
if n % i == 0:
return n-1
i+=1
You shouldn't return the first divisor you find, you should be adding them as you go; i takes on the values from 1 to n-2 in your loop change to for i in range(1,n); and there is no need to define an initial value for i. Try this instead:
def sums(n):
sum=0
for i in range (1, n):
if n % i == 0:
sum+=i
return sum

Categories

Resources