This question already has answers here:
Python, Make an iterative function into a recursive function
(4 answers)
Count to zero then Count up
(2 answers)
Closed 3 years ago.
I want to define a function as f(a,b) such that it generates a series as:
10,8,6,4,2,0,2,4,6,8,10 if a=10 and b=2 using Recursion.
def pattern(a,b):
if a-b < 0:
print(a)
return pattern(a+b,b)
print(a)
else:
print(a)
return pattern(a-b,b)
The result I get is
10
8
6
4
2
0
2
0
2
0
..... infinity
... but this is wrong.
You just need to use recursion
from __future__ import print_function
def psearch(a,b):
if a > 0:
print(a,end = ',')
psearch(a - b,b)
print(',',end="")
print(a,end = "")
else:
print(a,end="")
psearch(12,5)
print()
OUTPUT
12,7,2,-3,2,7,12
Related
This question already has answers here:
How can I time a code segment for testing performance with Pythons timeit?
(9 answers)
How do I profile a Python script?
(33 answers)
Closed 5 years ago.
I want to check how efficient my code is (time taken to run, and lines of code).
For example, how can I check whether this fibonacci code is efficient?
def fibonacci(start=0, end=10):
a = 0
b = 1
while True:
if b >= end:
break
a, b = b, a+b
if start <= a <= end:
print(a)
fibonacci(start=10, end=45)
This question already has answers here:
Comparing two lists using the greater than or less than operator
(3 answers)
Closed 5 years ago.
I want to compare two lists. For example:
a = [8,9,9,11]
b = [8,7,20,10]
if a >= b :
print "true"
Why does this print "true"? I want to compare the values vertically like this:
8 >= 8 is true
9 >= 7 is true
9 >= 20 is false but the program return true
11 >= 10 is true
You can use list comprehension and all function as follows:
code:
a = 8,9,9,11
b = 8,7,20,10
print all([(a > b) for a, b in zip(a,b)])
output:
False
You can use a list comprehension for comparing two lists element wise then use all function to check all of comparison are True:
a = [8,9,9,11]
b = [8,7,20,10]
all(a[i]>=b[i] for i in range(len(a))) # False
This question already has answers here:
How to count digits, letters, spaces for a string in Python?
(12 answers)
Closed 5 years ago.
how do I code a program that would produce this below?
As an example, the following code fragment:
print (count_letters("ab1c2d345"))
should produce the output:
4
You can try this:
def count_letters(s):
return len([i for i in s if i.isalpha()])
print(count_letters('ab1c2d345'))
Output:
4
Or, you can use regex for a cleaner solution:
import re
def count_letters(s):
return len(re.findall('[a-zA-Z]', s))
You can do it using simple loop/if statement.
def count_letters(str_ltr):
count_ltr = 0
for ch in str_ltr:
if ch.isalpha():
count_ltr += 1
return count_ltr
print(count_letters("ab1c2d345"))
Output: 4
This question already has answers here:
How can I iterate over overlapping (current, next) pairs of values from a list?
(12 answers)
Closed 6 years ago.
if given a command like:
python 1.py ab 2 34
How to print the next argument while you are currently sitting on the one before. e.g if x is ab then I want to print 2:
import sys
for x in sys.argv[1:]:
print next element after x
I am unsure what you want to print but this syntactic sugar can help:
for x in sys.argv[1::2]:
print x
This prints every other element. So this will return ab 34
for x in sys.argv[2::2]:
print x
This will return 2
The number after the :: is how many slots in the array.
Edit:
To answer your specific question:
val = 1
for index, x in enumerate(sys.argv[1::2]):
val += 1
print sys.argv[index+val]
The index increases by 1 each time, and the val by 1 too, meaning every loop we skip 2 variables. Hence for something like python yourcode.py a 1 b 2 c 3 d 4 output will be 1 2 3 4
This question already has answers here:
How do I count the occurrences of a list item?
(30 answers)
Closed 9 years ago.
I need help:
I want to write function that: takes a list as a input and return count of specific string.
If function input is x=[a,a,b,c] function need return 2 (a is two times in this list).
>>> def F(seq, string):
return seq.count(string)
>>> F(['a','a','b','c'], 'a')
2
Equivalent to:
def F(seq, string):
n = 0
for x in seq:
if x == string:
n += 1
return n