I want to make a function that would construct Lucas- (or Fibonacci-) like sequence. I want to be able to choose two first numbers and then to make the next one be a sum of two previous ones. The way I was thinking about that problem was:
def lucaslike(a, b, n):
x = [a, b]
for i in range(2, n+1):
return x.append(x[i-1] + x[i-2])
But when trying to use it print(lucaslike(3, 6, 10)), I get that the function is not defined.
I would like to be able to tell the function to produce Lucas-like sequence of n=10 terms, when for example two first values are a=3 and b=6. So that the output should be: [3, 6, 9, 15, 24, 39, ...].
You can change your code like this :
def lucaslike(a, b, n):
x = [a, b]
for i in range(2, n+1):
x.append(x[i-1] + x[i-2])
return x
Like that, this function will return only at the end the whole list of Lucas_like sentence.
lucaslike(3,6,10) or print(lucaslike(3,6,10)) will return [3, 6, 9, 15, 24, 39, 63, 102, 165, 267, 432]
Related
I am new to Python and I need help with following.
I do have a list a = [range(1, 50, 10)]
and another list b = [2, 4, 5, 8, 12, 34]
Now, I have a function that calculates something, let's call it "SomeFunction", that takes two arguments.
I want to pass to this SomeFunction for element from list a each element of list b. So, I want values for: SomeFunction(1, 2), SomeFunction(1, 4), SomeFunction(1, 5), ..... SomeFunction(50, 2), SomeFunction(50, 4), SomeFunction(50, 5), etc.
I think it should be done somehow with for loops, but I do not know how...
You'd need a nested for loop:
a = range(1, 50, 10)
b = [2, 4, 5, 8, 12, 34]
for aval in a:
for bval in b:
print(aval, bval) # or any other function call
This just goes through all values in b for each value in a. (Note that you don't want range inside square brackets as you have in your question, I removed those).
A more advanced version: "all pairs of values" is also known as the Cartesian product, which is provided by the itertools module. Thus you could write
from itertools import product
for aval, bval in product(a, b):
print(aval, bval)
Try this:
a = range(1, 50, 10)
b = [2, 4, 5, 8, 12, 34]
for i in a:
for n in b:
print(SomeFunction(i, n))
I am starting on my Python journey and am doing some exercises to get the hang of it all. One question is really giving me troubles as I do not understand how to complete it.
Question:
Given a list of natural numbers, remove from it all multiples of 2 (but not 2), multiples of 3 (but not 3), and so on, up to the multiples of 100, and print the remaining values.
From this question I take it that I should first make a list with all prime numbers and after that append the values that correspond to a new list. This is what I have until know:
# Read list:
a = [5, 6, 7, 8, 9]
# First get a list with all primes
primes = []
for i in range(0, 101):
for j in range(2, int(i ** 0.5) + 1):
if i%j == 0:
break
else:
primes.append(i)
# Next append to new list
b = []
for num in a:
for prime in primes:
if num == prime and num % prime == 0:
b.append(num)
print(b)
Now this works for this simple example, but upon testing it on another set (one of which I do not know the input values of the test case), my output is:
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
Which is my original list with the prime numbers. So somewhere along the line I am making a mistake and I cannot see the logic behind it. Any help would be greatly appreciated.
To solve the task all you need to do is filter out the numbers by using a generator and a for loop, which by itself is already finding the prime numbers
a = [5,6,7,8,9]
for i in range(2,101):
a = [j for j in a if (j==i or j%i!=0)]
I know how to do this using a loop, but interested what's a one liner pythonic way to do it.
For example, how would I get first 10 values of y for y = x^2 or y = log(x) for integers of x ?
eg for x^2 [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
List expression
ys = [x ** 2 for x in range(10)]
I guess you need the "map" function? for example:
# Python program to demonstrate working
# of map.
# Return double of n
def addition(n):
return n + n
# We double all numbers using map()
numbers = (1, 2, 3, 4)
result = map(addition, numbers)
print(list(result))
the output is:
[2, 4, 6, 8]
Above code from https://www.geeksforgeeks.org/python-map-function/
It seems sometimes this call to swap works and sometimes the return data doesn't change the array.
It seems to work with the smaller array but not the bigger one.
data = [1, 4, 5, 3, 2]
data = [1, 4, 5, 3, 2, 88, 92, 68, 22, 7,
6, 11, 44, 56, 99, 31, 32, 66, 55,
44, 77, 92, 24, 27, 14, 17, 18, 51,
69, 61, 51, 82, 84, 14, 15, 60, 70]
def Swap(a, b):
temp = a
a = b
b = temp
return {a, b}
def BubbleSort(data):
for i in range(0, len(data)-1):
for j in range(0, len(data)-1):
if data[j] > data[j+1]:
(data[j], data[j+1]) = Swap(data[j], data[j+1])
print("In data: ", data)
print("Before data: ", data)
BubbleSort(data)
print("After data: ", data)
Your Swap function returns a set, the order of which is unspecified ( implementation-defined). You probably want to return a tuple (return (a, b) or, omitting the redundant parentheses, return a, b).
But the function is unnecessary anyway. The canonical way for swapping two values in Python is
a, b = b, a
The Swap function returns a set, which has no defined order. Therefore, it is not guaranteed that it actually has the effect of swapping two items.
You probably meant to return a tuple, which is written as (a, b) or simply a, b.
Also, it is not necessary in Python to use a temporary variable to swap two values, due to tuple assignment.
The canonical way to write a swap function in Python is:
def swap(a, b):
return b, a
Note also the lower-case function name which follows the official PEP-8 style guide.
I have an array A
A = [5,2,8,14,6,13]
I want to get an array where each element is added to every other element, so the first five elements would be 5 + each element, then the next four would be 2 + each element etc.
So the result would be
B = [7,13,19,11,18, 10,16,8,15, 22,14,21, 20,27, 19]
What is the quickest way to do this without using for loops?
Note: The problem I am trying to solve involves large boolean arrays instead of integers and the actual operation is a boolean 'and', not merely addition. I have simplified the question for ease of explanation. I have been using for loops up to now, but I am looking for a faster alternative.
Use ` itertools.combinations
from itertools import combinations
a = [5,2,8,14,6,13]
print [sum(i) for i in list(combinations(a, 2))]
No need of list(). Thanks to #PeterWood
print [sum(i) for i in combinations(a, 2)]
Output:
[7, 13, 19, 11, 18, 10, 16, 8, 15, 22, 14, 21, 20, 27, 19]
Demo
You could do it recursively:
def add_value_to_rest(sequence):
if not sequence:
return []
else:
additional = sequence[0]
return ([additional + value for value in sequence] +
add_value_to_rest(sequence[1:]))
With generators, in Python 3:
def add_value_to_rest(sequence):
if sequence:
additional = sequence[0]
for value in sequence:
yield additional + value
yield from add_value_to_rest(sequence[1:])
Or with Python 2.7:
def add_value_to_rest(sequence):
if sequence:
additional = sequence[0]
for value in sequence:
yield additional + value
for value in add_value_to_rest(sequence[1:]):
yield value
A = [5,2,8,14,6,13]
B = []
for i, x in enumerate(A):
for l in range(len(A) - i - 1):
B.append(A[i] + A[i + l + 1])
print B
#[7, 13, 19, 11, 18, 10, 16, 8, 15, 22, 14, 21, 20, 27, 19]