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.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 days ago.
Improve this question
This is a program that multiplies each base value nums times
def calNums(base, *nums):
for i in range len(nums):
total = base
for j in nums-1:
total *= base
print(f'{}의 {} 제곱 값은 {}이다'.format(base, nums, total))
calNums(5, 1, 2, 3)
calNums(2, 2, 4, 6, 8, 10)
I have a problem with 2nd line.
The problem here is range is not a keyword, it requires you to use parentheses.
Line 5:
for i in range(len(nums)):
And I have absolutely no idea what you're trying to do here.
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 10 months ago.
Improve this question
def solution(A):
for item in range(0, 1000000 + 1):
if item > 0:
if item not in A:
return item
A = [1, 3, 6, 4, 1, 2]
print(solution(A))
You should explicitly state the item again after the and statement, so the line should be
if item > 0 and item not in A:
Also, you're missing logic within your if statement, there should be at least one line of indented code following it.
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.
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 5 years ago.
Improve this question
I need an example of a recursive function that will append all integer elements of a number into a list using python and doing this with the most basic algorithm.
For example if n = 1234, list will be [1,2,3,4].
Try this?
def numberToList(number):
# base case
if number == 0:
return []
# recurse
return numberToList(number / 10) + [ number % 10 ]
Run it like this:
>>> numberToList(1234)
[4, 3, 2, 1]
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 6 years ago.
Improve this question
I know the similar question has been asked before and I looked through a lot of previous questions but couldn't find the answer hence I am posting it.
I have a basic question about for loop. I have been using VBA for a long time and recently have to learn Python for work. I will need to use FOR LOOP in the middle of the code and looks like there in no NEXT following the FOR in python. For example, this is what the code looks like in VBA:
sub test()
dim i as integer
dim j as integer
for I = 1 to 10
j = j+i
Next
.
Print(j)
.
.
'rest of the code
end
Can someone please tell me how to define the FOR LOOP in the middle of the code? Thanks
Python alternative for your function
def test():
for i in range(1, 10):
print(i)
Note out that python has no variable declaration as it is not strongly-typed language.
Also instead of next it has indentation as depth of execution.
In python for loop are used over iterable object or generator.
List:
num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for x in num:
print x
Generator:
for x in range(10):
print x
For more example, see https://wiki.python.org/moin/ForLoop
Your question not very clear, so if this does not answer your question do not hesitate to edit your message.
This will also do your job.
for i in range(10): # from 0 to 9.
j = j + i