Python : parasitic number - python

In general, a positive natural number that can be multiplied by n
by moving the rightmost digit to the front of the number is called an n
-parasitic number. Here n is itself a single-digit positive natural number. For example: 4×128205=512820
4×128205=512820
so 128205 is a 4-parasitic number. Natural numbers with leading zeros are not allowed. So even though
4×025641=102564
4×025641=102564
the number 025641 is not 4-parasitic.
Assignment: write a function parasitic that takes a natural number. In case the given natural number is n-parasitic, the function must return the value n. Otherwise, the function must return the value 0.
My code (last definition parastic(number)) is not working for some cases for example: parasitic(142857)
n = 5 while with my code I return 0.
def rotateLeft(number):
"""
>>> rotateLeft(717948)
179487
>>> rotateLeft(142857)
428571
>>> rotateLeft(105263157894736842)
52631578947368421
"""
k = str(number)
letter = k[:1]
numb = k[1:]
resultaat = str(numb) + str(letter)
return int(resultaat)
def rotateRight(number):
"""
>>> rotateRight(179487)
717948
>>> rotateRight(428571)
142857
>>> rotateRight(52631578947368421)
15263157894736842
"""
k = str(number)
letter = k[-1]
numb = k[:-1]
resultaat = str(letter) + str(numb)
return int(resultaat)
def parasitic(number):
"""
>>> parasitic(179487)
4
>>> parasitic(142857)
5
>>> parasitic(105263157894736842)
2
>>> parasitic(1234)
0
"""
count = 0
getal = count * number
while getal != rotateLeft(number):
count += 1
getal = count * number
if getal == rotateLeft(number):
break
return (count)
else:
return 0

While using a while loop may improve your grasp of python, this can be solved rather simply using the % operator.
def rotateRight(number):
"""
>>> rotateRight(179487)
717948
>>> rotateRight(428571)
142857
>>> rotateRight(52631578947368421)
15263157894736842
"""
k = str(number)
return int(k[-1] + k[:-1])
def parasitic(number):
rotated = rotateRight(number)
if not rotated % number:
return rotated // number
else:
return 0
This tests to see if number is divisible by the number obtained by a right-rotation, and, if so, returns the divisor (the // operator rounds the result to the nearest integer, but we already know the result must be an integer)

I have written a function that seems to do the job for your examples!
It uses concatenation of string slices and compares this against the num. I think a trick is that n can only ever be between 2 and 11 for this to work as otherwise the shift by 1 at the end would never make the original number.
Here's the code:
def parasitic(num):
for n in range(2, 11):
res = n * num
if str(num)[-1] + str(num)[:-1] == str(res):
return n
return False
Testing:
>>> parasitic(128205)
4
>>> parasitic(142857)
5
>>> parasitic(105263157894736842)
2
>>> parasitic(35)
False

Your issue (having removed break, which is the first issue as it ends before it can return anything):
if getal==rotateLeft(number):
return count
else:
return 0
I think you intended a while-else loop, but you ended up with "if the first result doesn't work, return 0". You need to change indentation so the else lines up the the while (so if no result is found, return 0), not the if. You will also have to add a limit of what to check, or if there is no result it will go on forever.
while getal != rotateLeft(number):
count += 1
getal = count * number
if getal == rotateLeft(number):
return (count)
else:
return 0

If your number is parasitic, the while loop will eventually stop even without you put break in it. I would suggest not to use while loop with this current conditon, since this will be infinity-loop if the number is not parasitic.
But I may show that your code would work if you remove the return in else condition. And also you may want to add both condition of rotateLeft and rotateRight :
def parasitic(number):
count = 0;
getal = count * number;
while getal != rotateLeft(number) and getal != rotateRight(number) :
count += 1
getal = int(count * number)
if getal == rotateLeft(number) or getal == rotateRight(number) :
print(count); print(getal);
return (count)
else:
pass
Hope this helps.

if getal == rotateLeft(number):
break
return (count)
Since break leaves the loop, you cannot ever reach the return statement; your code would simply fall off the end of the function, and return None.
Also, your double-check of getal == rotateLeft(number) signals poor loop design; you should be able to test this once at the top of the loop, without making a second check within.
REPAIR
You have to handle two cases:
If you find a multiplier that matches the rotation, return that multiplier.
If all valid multipliers fail, then you return 0.
So (a) how many multipliers do you try before you give up?
(b) where do you put the two different return statements to handle those cases?

Related

The incrementing here continues to return a 0 value

I was writing the solution to this codewars problem however I've ran into a bit of an issue.
Problem statement:
Write a function, persistence, that takes in a positive parameter num and returns its multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a single digit, e.g.:
persistence(39) # returns 3, because 39=27, 27=14, 1*4=4 and 4 has only one digit
def persistence(n, t=1, x=0):
if len(str(n)) > 1:
number = [int(i) for i in str(n)]
for i in number:
t = t * i
if len(str(t)) > 1:
x += 1
return(persistence(t,x))
else:
return(x)
else:
return 0
I can't quite figure out what the error is in this code. My hunch is that it's either a parameter error or the way the return() value is placed.
In essence, the code for distilling an integer to it's multiples is correct, so I just added an extra parameter to persistence; setting x = 0 and making it so that each time the if condition was fulfilled it would increment that exact x value. Once the number was distilled, simply output x. Yet it continues to simply output 0 as the final answer. What's the problem here?
Edit: Solution was in the comments, didn't realise how the parameters were passing. Correct version is:
return(persistence(t,1,x))
Also had to set x = 1 for the logic to work on codewars.
There are 2 flaws in Your code:
return(persistence(t,x))
should be
return(persistence(t,1,x))
otherwise the value of x will be assigned to t and x will be defaulted to 0.
Then you must increment x directly after the first test, otherwise You will miss one iteration.
Another way to calculate this is not to switch over to strings, but to do it numerically:
def persistence(n):
iterations = 0; # no iterations yet
while n > 9: # while n has more than 1 digit:
product = 1 # neutrum for result product
while n > 0: # while there a digit to process:
digit = n % 10 # retrieve the right most digit
product *= digit # do the multiplication
n = n // 10 # cut off the processed digit
iterations += 1 # increment iterations
n = product # let n be the newly calculated product
return iterations # return the result
I think you your function's parameters work not as you expect them to do.
When you call function persistence(t, x), the first argument n should become t, and second argument x, should become new x. But in your function, x becomes new t because of their position.
It is quite useful to have bunch of print statements to reveal the bug.
def persistence(n, x=1, t=1):
print('x:', x)
if len(str(n)) > 1:
number = [int(i) for i in str(n)]
for i in number:
t = t * i
print('t:', t)
if len(str(t)) > 1:
x += 1
print('x has changed:', x)
return persistence(t, x)
else:
return x
else:
return 0
print(persistence(39))
print('---------------')
print(persistence(999))
print('---------------')
print(persistence(4))
Passes all test cases with two changes:
You were not updating your n with the new t everytime
Your x was being set to 0 every time. That should be set to 1 in the beginning (default value)
def persistence(n, t=1, x=1):
if len(str(n)) > 1:
number = [int(i) for i in str(n)]
for i in number:
t = t * i
if len(str(t)) > 1:
x += 1
return (persistence(n=t, x=x))
else:
return (x)
else:
return 0
Actually, you can write it without needing both parameters t and n. Just one n is fine as shown below:
def persistence(n, x=1):
if len(str(n)) > 1:
number = [int(i) for i in str(n)]
t = 1
for i in number:
t = t * i
if len(str(t)) > 1:
return x + (persistence(n=t, x=x))
else:
return (x)
else:
return 0

Finding how many times a certain character appears in a multiplied string

We want to find the number of 'a's in a given string s multiplied infinite times.
We will be given a number n that is the slicing size of the infinite string.
sample input:
aba 10
output:
7
Here aba is multiplied with 10, resulting in 'abaabaabaa'
and the no. of 'a's are 7.
This is my code:
def repeatedString(s, n):
count = 0
inters = s * n
reals = s[0:n+1]
for i in reals:
if (i == 'a'):
count += 1
return count
I'm getting 2 instead of 7 as the output (test case 'aba' 10). Where did I go wrong? I just multiplied the given string with n because it will never be greater than the slicing size.
Here's the link to the problem:
https://www.hackerrank.com/challenges/repeated-string/problem
Much simpler solution using python3.
s = input().strip()
n = int(input())
print(s[:n%len(s)].count('a')+(s.count('a')*(n//len(s))))
There's no reason to slice the string
def repeatedString(s, n):
count = 0
for index, i in enumerate(s*n):
if index >= n:
return count
if(i == 'a'):
count += 1
# empty string
return count
I used a simple unitary method.
Number of 'a' in one repetition is cnt_a so the number of 'a' in first n characters will be (cnt_a/len(s)) * n
def repeatedString(s, n):
if len(s)==1 and s=='a':
return n
cnt_a=0
for i in s:
if i == 'a':
cnt_a+=1
if cnt_a % 2 == 0:
no_a = (cnt_a/len(s)) * n
return math.ceil(no_a)
else:
no_a = (cnt_a/len(s)) * n
return math.floor(no_a)
If you would like a more readable answer....
def repeatedString(s, n):
target = 'a'
target_count = 0
# how many times does the string need to be repeated: (n // len(s) * s) + s[:(n % len(s))]
quotient = n // len(s)
remainder = n % len(s)
for char in s: # how many times target appears in 1 instance of the substring
if char == target:
target_count += 1
# how many times the target appears in many instances of the substring provided
target_count = target_count * quotient
for char in s[:remainder]: # count the remaining targets in the truncated substring
if char == target:
target_count += 1
return target_count
One liner answer:
return [s[i%len(s)] for i in range(n)].count('a')
There is only two problem in your code
s = 'aba'
n = 10
count = 0
inters = s * n
# Here you need to slice(inters) not (s) because s only hold 'aba'
# And not n+1 it's take 11 values only n
reals = inters[0:n]
for i in reals:
if (i == 'a'):
count += 1
print(count)
so if the string contains "a"s only simply return n. otherwise, count the number of a's in the string s, now using divmond() function I have found the number of string that can be added without surpassing n. for example string s is "aba" and n=10, so I can add 3 "abs"s completely without the length of string going over 10. now the number of a's in the added string (3*2). Now the places left to be filled are equal to the remainder(y) of divmond() function. Now slice the string s up to y and find the number of a's in it and add it to count.
divmond(10,3) returns (10//3) and it's remainder.
def repeatedString(s, n):
if len(s)==1 and s=="a":
return n
count=s.count("a")
x,y=divmod(n,len(s))
count=count*x
str=s[:y]
return count+str.count("a")
The solution in Python 3:
def repeatedString(s,n):
i = 0
c = 0
for i in s:
if i == 'a':
c += 1
q = int(n / len(s)) #Finding the quotient
r = int(n % len(s)) #Finding the remainder
if r == 0:
c *= q
else:
x = 0
for i in range(r):
if s[i] == 'a':
x += 1
c = c*q + x
return int(c)
s = input()
n = int(input())
print(repeatedString(s,n))
if character 'a' is present in a given string pattern, then its quite faster to get the repeated count for it and later based on the total length of final string mentioned, will be trying to repeat the given pattern for same number of times & hence will multiple the repeated count with number of times a string pattern is going to repeat. Importantly if final string input is in odd numbers then we need to identify the those odd pattern and separately count the occurance of character 'a' in odd string pattern. Finally summing up the total count ( even & odd ) will gives us the expected result
def repeatedString(s, n):
# Get the length of input string
strlen = len(s)
a_repeat = 0
# Get the total count of a repeated character from the input string
for i in range(0,strlen):
if s[i] == 'a':
a_repeat = a_repeat + 1
# Get the multiplier to make sure that desired input string length achieved
str_multiplier = int(n // strlen)
# Get the repeated count if new string is been created
result = a_repeat*str_multiplier
new_str = s[:int( n % strlen )]
# for odd length of string, get the remaining characters and find repated characters count and add up it to final count
for i in range(0, len(new_str)):
if new_str[i] == 'a':
result += 1
return result
For this problem,
Get the length of string s.
First, if conditions: constrain
Now, instead of using a loop to add to space and time complexity, we use basic math's. Find the quotient of n//Len (s). Now find the number of times "a" is used in our string.
We can multiply the quotient with this number to get the total "a" used. Now, we can find the remainder of the string and use slice to search for "a" in the string we have left in the last.
Add both to get our answer.
def repeatedString(s, n):
#finding quotient and remainder of division
str1=len(s)
remainder=0
if 1<=str1<=100 and 1<=n<=10**12:
quotient= n//str1
a_s = s.count("a")
if a_s==0:
return 0
else:
remainder=s[:n%str1].count('a')
return quotient*a_s + remainder
Simple answer:
def repeatedString(s, n):
totalNumber = 0 // setting total of a's to 0
// using count function to find the total number of a's in the substring
totalNumber = s.count('a')
// finding how many number of times the substring fits in "n" and multiplying that by the number of a's we found earlier
totalNumber = n//len(s) * totalNumber
// if there is a remainder, we loop through the remainder string and add the number of "a's" found in that substring to the total
for i in s[:n%len(s)]:
if(i == "a"):
totalNumber +=1
return totalNumber

Trying to write a code that will find the sum of the even numbers of the Fibonacci sequence?

I'm new to programming and I'm trying to write a program in Python that will find the sum of the even numbers of the numbers below 4,000,000 in the Fibonacci sequence. I'm not sure what I'm doing wrong but nothing will print. Thanks for any help.
def fib():
listx = []
for x in range(4000000):
if x == 0:
return 1
elif x == 1:
return 1
else:
listx.append(fib(x - 1) + fib(x - 2))
return listx
def evens(fib):
y = 0
for x in fib():
if x % 2 == 0:
y += x
else:
continue
print (y)
Here's an approach that uses a generator to keep memory usage to a minimum:
def fib_gen(up_to):
n, m = 0, 1
while n <= up_to:
yield n
n, m = m, n + m
total = 0
for f in fib_gen(4000000):
if f % 2 == 0:
total += f
Another option:
def fib_gen(up_to, filter):
n, m = 0, 1
while n <= up_to:
if filter(n):
yield n
n, m = m, n + m
sum(fib_gen(4000000, lambda f: f % 2 == 0)) # sum of evens
sum(fib_gen(4000000, lambda f: f % 2)) # sum of odds
First things first, there appears to be some contention between your requirements and the code you've delivered :-) The text of your question (presumably taken from an assignment, or Euler #2) requests the ...
sum of the even numbers of the numbers below 4,000,000 in the Fibonacci sequence.
Your code is summing the even numbers from the first four million Fibonacci numbers which is vastly different. The four millionth Fibonacci number has, according to Binet's formula, north of 800,000 digits in it (as opposed to the seven digits in the highest one below four million).
So, assuming the text to be more correct than the code, you don't actually need to construct a list and then evaluate every item in it, that's rather wasteful on memory.
The Fibonacci numbers can be generated on the fly and then simply accumulated if they're even. It's also far more useful to be able to use an arbitrary method to accumulate the numbers, something like the following:
def sumFibWithCond(limit, callback):
# Set up initial conditions.
grandparent, parent, child = 0, 0, 1
accum = 0
# Loop until number is at or beyond limit.
while child < limit:
# Add any suitable number to the accumulator.
accum = accum + callback(child)
# Set up next Fibonacci cycle.
grandparent, parent = parent, child
child = grandparent + child
# Return accumulator when done.
return accum
def accumulateEvens(num):
# Return even numbers as-is, zero for odd numbers.
if num % 2 == 0:
return num
return 0
sumEvensBelowFourMillion = sumFibWithCond(4000000, accumulateEvens)
Of special note is the initial conditions. The numbers are initialised to 0, 0, 1 since we want to ensure we check every Fibonacci number (in child) for the accumulating condition. This means the initial value of child should be one assuming, as per the question, that's the first number you want.
This doesn't make any difference in the current scenario since one is not even but, were you to change the accumulating condition to "odd numbers" (or any other condition that allowed for one), it would make a difference.
And, if you'd prefer to subscribe to the Fibonacci sequence starting with zero, the starting values should be 0, 1, 0 instead.
Maybe this will help you.
def sumOfEvenFibs():
# a,b,c in the Fibonacci sequence
a = 1
b = 1
result = 0
while b < 4000000:
if b % 2 == 0:
result += b
c = a + b
a = b
b = c
return result

Sum of odd numbers using while loop in python

I'm new to programming and was asked to sum odd numbers from 1 to (2*n)-1 using a while loop.
This is my attempt:
def sum_odd_n(n):
while n<2*n:
sum = 0
if n%2==1:
sum = sum+n
return (sum)
May i know my mistake? Any help will be appreciated
The condition while n<2*n: is always true while n >= 0, you'll have an infinite loop. Try the following
def sum_odd(n):
value = 1
total = 0
while value < (2*n) - 1:
if value % 2 == 1:
total += value
value += 1
return total
>>> sum_odd(25)
576
For completeness the more pythonic way to handle this would be using sum with a generator expression
def sum_odd(n):
return sum(i for i in range(1, 2*n -1) if i%2 == 1)
The first hint would be to take a look at your condition in while loop:
while n < 2*n
Notice that this will always be true, because if n>0, then 2*n is always greater. If you need more help, write a comment ;)
UPDATE: I will just tell you what is wrong so if you want to try it out first on your own, stop reading. So basically you need another variable, let's say i that will loop through integers. Then you can do something like this:
def sum_odd_n(n):
i = n
sum = 0
while i < 2*n:
if i % 2 == 1:
sum += i
i += 1
print sum # for python3: print(sum)
>>> def sum_odd_n(n):
... return sum([2 ** i for i in range(0,n,1) ])
...
>>> sum_odd_n(2)
3
>>> sum_odd_n(5)
31
>>>
I'll try to point out the mistakes you have done, and try to offer corrections.
def sum_odd_n(n):
while n<2*n: # n will always be less than ('<') 2*n. For eg, if n=5, 5<10.
sum = 0 # you are resetting the sum at every iteration. We need to take this outside.
if n%2==1:
sum = sum+n
return (sum)
Here's what I think should work.
def sum_odd_n(n):
sum = 0 # sum is initialized here, so that it doesn't reset inside the loop
iterator = 0
while iterator<2*n
if iterator%2==1:
sum = sum+iterator # or sum += iterator
iterator = iterator + 1 # otherwise this will be an infinite loop, as iterator will always be 0.
return sum
Hope this works for you. :-)
This worked for me:
def sum_odd(n: int):
total = 0
for x in range(2*n):
if x%2==1:
total += x
return total

How do I do this "If n is 3, return `1+..1+2+..1+2+3+..`"

Define a function named nested_increasing_additions(n) which receives one positive integer (n) and returns a string as illustrated in the following examples:
If n is 3, the function should return the string:
1+..1+2+..1+2+3+..
If n is 5, the function should return the string:
1+..1+2+..1+2+3+..1+2+3+4+..1+2+3+4+5..+
What I think is, I can make n to a list [1,2,3] and use while loop or for loop to repeat n times. For the first loop it returns 1+.., for the second loop it returns 1+2.. somehow (which i don't know) it stops at 2 which is the same as the repeating time.
I don't know if I'm thinking it right. Need some help and explanations! Thank you!
Consecutive evaluations of these strings results in a sequence of tetrahedral numbers. For example, for input 5, the output evaluates to 35. This is the number of spheres you would need to build a tetrahedron of side length 5.
To see how it relates to the sum in the question, note that the discrete "volume" of the tetrahedron would be equal to the sum of the triangle "slices" from top to bottom.
35 = 1 + 3 + 6 + 10 + 15
= 1 + (1+2) + (1+2+3) + (1+2+3+4) + (1+2+3+4+5)
By a similar argument, the triangular numbers are made up of slices of consecutive integers.
Please excuse the maths, it was difficult (but not impossible) to adapt a closed-form solution into the desired output format.
def tetrahedral(n):
return n*(n+1)*(n+2)//6
def string_generator(n):
x = tetrahedral(n)
n = N = 1
while x > 0:
while n <= N:
yield str(n) + '+'
n += 1
x -= N*(N+1)//2
n = 1
N += 1
yield '..'
def nested_increasing_additions(n):
return ''.join(string_generator(n))
You can build the complete string step by step, and remember at each step what you have added last:
def nested_increasing_additions(n):
complete_string = ""
add_string = ""
for i in range(1,n+1):
add_string += str(i) + "+"
complete_string += add_string + ".."
return complete_string
print(nested_increasing_additions(1))
print(nested_increasing_additions(3))
print(nested_increasing_additions(5))
The output with python3 is:
1+..
1+..1+2+..1+2+3+..
1+..1+2+..1+2+3+..1+2+3+4+..1+2+3+4+5+..
def nested_increasing_additions(n):
l=['']
for i in range(1, n+1):
l.append(l[-1]+str(i)+'+')
return '..'.join(l[1:])
This returns a string without the .. at the end. If you want that, just do return '..'.join(l[1:]) + '..'
you can use something like this.
def nested_increasing_additions(n):
string = ""
for i in range(1,n+1): #1
for j in range(1,i+1): #2
string += str(j)+"+" #4
string += ".." #5
print(string)
here is a printout of nested_increasing_additions(4)
1+..1+2+..1+2+3+..1+2+3+4+..
i think it's self explanatory, nothing complicated.
How's this:
def nested_increasing_additions(n):
string = ""
new_string = ""
dot = ".."
for i in range(1, n+1):
new_string+=('{}+'.format(i))
string = string+new_string+dot
print(string)
return (string)
Output:
nested_increasing_additions(3)
'1+..1+2+..1+2+3+..'
Assuming you did want the ".." at the end of each returned string and that recursion is OK, here's a solution:
def nested_increasing_additions(n):
if n == 1:
return "1+.."
else:
return nested_increasing_additions(n-1) + '%s+..' % '+'.join(str(i) for i in range(1, n+1))
print(nested_increasing_additions(3))
print(nested_increasing_additions(5))
type(nested_increasing_additions(1))
Prints:
1+..1+2+..1+2+3+..
1+..1+2+..1+2+3+..1+2+3+4+..1+2+3+4+5+..
<type 'str'>
Explanation:
The first return and if (true) block ends the recursive call when the passed in argument value reaches 1 via subtraction from the other half.
The second half (else block) calls the next iteration with n subtracted by 1 and the string build of the current iteration (if n!=1).
The complicated looking code '%s+..' % '+'.join(str(i) for i in range(1, n+1)) is just concatenation of a list of numbers generated by the range function, turned into strings and "+.."
range(1, n+1) returns a list of integers starting with 1 until n+1 so range(1,3) yields [1,2]. This is passed to join which places a + between each number.

Categories