This code checks whether or not a combination of numbers produces a vampire number but returns an incorrect True if x = 2947051 and y = 8469153.
def vampire_test(x, y):
vamp = (list(str(x) + str(y)))
prod = x*y
vamp_check = list(str(prod))
print vamp, vamp_check
if '-' in vamp and '-' not in vamp_check:
return False
else:
check = cmp(vamp.sort(),vamp_check.sort())
if check == 0 and len(vamp) == len(vamp_check):
return True
else:
return False
What's the issue and how can I improve the code I've already written?
There is a logic problem. This line:
check = cmp(vamp.sort(),vamp_check.sort())
Will do check = 0 every time, because the .sort() method of list sorts in place and returns None.
Related
Here is my python code:
class Solution():
def isPalindrome(self):
return str(self.x) == str(self.x)[::-1]
s1 = Solution()
s1.x = 121
s1.isPalindrome()
It checks to see if the input is a palindrome. I want to create a new object that has the x value 121 and when I execute the isPalindrom function, I want it to return either a true or false boolean answer.
Currently when I run this program, nothing gets outputted. I am a bit lost as to where to go from here, would appreciate help.
Just print out the return value of isPalindrome(), because if you have a line with only a return value (this case being a boolean), the compiler won't know what to do with it.
class Solution():
def isPalindrome(self):
return str(self.x) == str(self.x)[::-1]
s1 = Solution()
s1.x = 121
print(s1.isPalindrome())
You're not telling the program to print anything. Try using print to make it reveal the answer.
Along with printing results we can also make class more pythonic.
class Solution:
def __init__(self):
self.input = None
def is_palindrome(self):
if isinstance(self.input, str):
return self.input == self.input[::-1]
print("Error: Expects str input")
return False # or leave blank to return None
s1 = Solution()
print(s1.is_palindrome())
s1.input = "121"
print(s1.is_palindrome())
output
Error: Expects str input
False
True
The main idea here is divide number. let's take number 122. First of all you need store it in a variable, in this case r_num. While loop is used and the last digit of the number is obtained by using the modulus operator %. The last digit 2 is then stored at the one’s place, second last at the ten’s place and so on. The last digit is then removed by truly dividing the number with 10, here we use //. And lastly the reverse of the number is then compared with the integer value stored in the temporary variable tmp if both are equal, the number is a palindrome, otherwise it is not a palindrome.
def ispalindrom(x):
r_num = 0
tmp = x
while tmp > 0:
r_num = (r_num * 10) + tmp % 10
tmp = tmp // 10
if x == r_num:
return True
return False
Outline:
Find out if id is acceptable. Acceptable parameters is the sum of the
digits for each part of the id. If each sum is evenly divisible by 10
then the function returns the string "Acceptable", otherwise it
returns the string "Unacceptable".
Example:
isValid('123-12-134') → 'Unacceptable'
isValid('550-55-055') → 'Acceptable'
isValid('123-55-055') → 'Unacceptable'
I've tried converting the entire string into an int, but get some differing results in determining divisible by 10.
My attempted code is:
def isValid(id) :
id=id.replace('-','0')
id=int(id)
if id % 10==0:
return "Valid"
else:
return "Invalid"
Thanks in advance!
You might as well return boolean variables and just compare the output to determine what to print:
def summation(item):
return sum([int(i) for i in item])
def isValid(id_) :
id_part = id_.split('-')
result = [summation(item) % 10 == 0 for item in id_part]
return all(result)
Essentially this loops through all the characters in the split string and determines their sum - 3 sums per provided id.
Then we convert the summed list to a boolean list using your condition of x%10 == 0.
Finally we look at all() the elements of this boolean list to determine if it all True or contains a False.
If all are True then the return of isValid(id_) is True else it is False.
Usage:
ids = ['123-12-134', '550-55-055', '123-55-055']
for id_ in ids:
validity = isValid(id_)
print("Acceptable") if validity else print("Unacceptable")
Output:
Unacceptable
Acceptable
Unacceptable
you mean like this?
sentence = "123-123-123"
a = sum(int(x) for x in sentence if x.isdigit())
Hope this code can help you.
Found on this answer
you mean like this?
sentence = "123-123-123"
a = sum(int(x) for x in sentence if x.isdigit())
return a % 10 == 0
Hope this code can help you.
Found on this answer
We want to short-circuit the 'Unacceptable'.
def isValid(ID):
s = 0
for x in ID:
if x.isdigit():
s += int(x)
else:
if s % 10 == 0:
s = 0
else:
return 'Unacceptable'
return 'Acceptable' if s%10 == 0 else 'Unacceptable'
The solution requires splitting the string into parts using hyphens as separators, which are tested to ensure that the sum of each part's characters is a multiple of 10. The test fails if any of the parts are not a multiple of ten, so each part must be greater than or equal to ten. If any part fails, the string fails, so, there is no need to continue testing if a failed part is found. Acceptable must be returned if the string passes, or Unacceptable if it fails.
This single function solution is easy to read:
def teststring(test):
for part in test.split('-'):
part_failed = int(part)<10
if not part_failed:
sum_chars = 0
for char in part:
sum_chars += int(char)
part_failed = ((sum_chars % 10) != 0)
if part_failed: break
return 'Acceptable' if not part_failed else 'Unacceptable'
This solution uses list comprehension in two functions:
def testpart_comprehended(part):
return ((int(part)>=10) and ((sum(int(char) for char in part) % 10) == 0))
def acceptable_comprehended(test):
return 'Acceptable' if all(testpart_comprehended(part) for part in test.split("-")) else 'Unacceptable'
This solution uses list comprehension in one function:
def all_comprehended(test):
return 'Acceptable' if all(((int(part)>=10) and ((sum(int(char) for char in part) % 10) == 0)) for part in test.split("-")) else 'Unacceptable'
These answers are all too understandable. Please use
isValid = lambda x: (any(sum(map(int, s)) % 10 for s in x.split('-'))
* 'un' + 'acceptable').title()
Unacceptable
for example
>>> isValid('123-123')
'Unacceptable'
>>> isValid('123-127')
'Unacceptable'
>>> isValid('127-127')
'Acceptable'
The problem is a homework problem. I think I solved it but I'm wondering is it the best solution? The problem is to take a list and run through it to see if it contains a specific subsequence: 007. Here subsequence is used in the mathematical sense (so 0110227 does contain 007 and returns True).
def spy_game(nums):
if 0 in nums:
a_1=nums.index(0)
nums_1=nums[a_1+1:]
if 0 in nums_1:
a_2=nums_1.index(0)
if 7 in nums[a_2+1:]:
return True
else:
return False
else:
return False
else:
return False
Start by negating your test condition so you can simply return early. The last test doesn't require an if statement at all, since by that point the result of the condition is the return value of the function.
def spy_game(nums):
if 0 not in nums:
return False
a_1 = nums.index(0)
nums_1 = nums[a_1+1:]
if 0 not in nums_1:
return False
a_2 = nums_1.index(0)
return 7 in nums[a_2+1:]
If you negate your inclusion conditions you can return early rather than introducing further nesting.
def spy_game(nums):
if 0 not in nums:
return False
a_1 = nums.index(0)
num_1 = nums[a_1+1:]
if 0 not in nums_1:
return False
a_2 = nums_1.index(0)
return 7 in nums_1[a_2+1:]
You can also write this without creating any copies of the list by using the start parameter to the index method, which will tell the index method the index at which to start searching for the element. This code looks different but achieves the same thing and may be easier to maintain for you.
def spy_game(nums):
try:
start = 0
for n in [0, 0, 7]:
start = nums.index(n, start)
return True
except ValueError:
return False
It's better to design your functions so that they don't depend on the specific input. For example:
def contains_anywhere(s, search):
i = 0
for c in search:
try:
i = s.index(c, i) + 1
except ValueError:
return False
return True
ok = contains_anywhere([0,1,1,0,2,2,7,2], [0,0,7])
Just another option, I personally don't see an issue with the nesting here.
This is shortening it up:
def spy_game(nums):
if 0 in nums:
nums_1 = nums[nums.index(0)+1:]
if 0 in nums_1:
if 7 in nums[nums_1.index(0)+1:]:
return True
return False
output:
>>> spy_game([1,0,7])
False
>>> spy_game([0,1,7])
False
>>> spy_game([1,0,1])
False
>>> spy_game([0,0,7])
True
Your code hides what you are trying to do. Try this alternative. even though it still has 3 levels of nesting, it is clearer about what it is doing:
def spy_game(nums):
search_for = [x for x in "007"]
for n in nums:
if not search_for: # found everything we were looking for
return True
if n == search_for[0]: # found the next item
search_for.pop(0)
return False
I'm implementing the dichotomic search algorithm in python, in a second version of the function I have to (in addition of returning true or false according to the presence or not of the element) count the number of operations (comparisons) done by the algorithm depending on the length of the list I'm working with and return it.
However, my function is recursive and naturally I'll have to initialize a counter variable (which will be incremented at every operation) to zero. the issue is that this variable will take the zero value at every recursive call and thus, it will not give me the correct value. I thought of a global variable but I don't know how to use it.
Here is the code of my function :
def trouve(T, x) :
if len(T) == 0 :
return False
mid = len(T) // 2
if T[mid] == x :
return True
if len(T) == 1 and T[0] != x :
return False
else :
if x > T[mid] :
return trouve(T[mid:], x)
else :
return trouve(T[:mid], x)
Normally, you would count the comparisons of data only, so not the conditions where you compare the length of the input list.
You could use a third argument to accumulate the count, and then let the function return a tuple of both the success and the count:
def trouve(T, x, c = 0):
if len(T) == 0:
return (False, c) # len() comparisons do not count
mid = len(T) // 2
if T[mid] == x:
return (True, c+1)
if len(T) == 1: # you don't need to compare x again here!
return (False, c+1)
# you don't need `else` here
if x > T[mid]:
return trouve(T[mid:], x, c+2)
# you don't need `else` here
return trouve(T[:mid], x, c+2)
print (trouve([1,3,8,13,14,15,20], 14))
Note that you can optimise a bit:
def trouve(T, x, c = 0):
if len(T) == 0:
return (False, c)
mid = len(T) // 2
# you don't need the `len(T) == 1` case, as it can be
# treated in the recursive call. See change below:
if x > T[mid]:
return trouve(T[mid+1:], x, c+1) # exclude mid itself
# Move equality test below greater-then test, since the
# equality has little chance of being true:
if T[mid] == x:
return (True, c+2)
return trouve(T[:mid], x, c+2)
print (trouve([1,3,8,13,14,15,20], 14))
... although for the example I gave, the count is still the same in this version.
If you want to go the global variable route (since you mentioned it), this is how you would do it.
trouve_count = 0
def trouve(T, x) :
global trouve_count
# Increment trouve_count like this when necessary:
trouve_count += 1
# ...
Be careful using these in larger programs, as you may accidentally use the same global name twice, causing problems.
In python, I am trying to check if a given list of values is currently sorted in increasing order and if there are adjacent duplicates in the list. If there are, the code should return True. I am not sure why this code does not work. Any ideas? Thanks in advance!!
def main():
values = [1, 4, 9, 16, 25]
print("Return true if list is currently sorted in increasing order: ", increasingorder(values))
print("Return true if list contains two adjacent duplicate elements: ", twoadjacentduplicates(values))
def increasingorder(values):
hlist = values
a = hlist.sort()
if a == hlist:
return True
else:
return False
def twoadjacentduplicates(values):
ilist = values
true = 0
for i in range(1, len(ilist)-1):
if ilist[i] == ilist[i - 1] or ilist[i] == ilist[i + 1] :
true = true + 1
if true == 0:
return False
if true > 0:
return True
main()
Your increasingorder function will almost certainly not work, because Python uses references, and the sort function modifies a list in-place and returns None. That means that after your call a = hlist.sort(), both hlist will be sorted and a will be None. so they will not compare equal.
You probably meant to do the following, which will return a sorted list instead.
a = sorted(hlist)
This function works:
def increasingorder(values):
hlist = values
a = sorted(hlist)
if a == hlist:
return True
else:
return False
You can of course simplify this down to a single line.
def increasingorder(values):
return sorted(values) == values
Your second function looks logically correct, but can be simplified down to the following.
def twoadjacentduplicates(values):
for i in range(0, len(values)-1):
if values[i] == values[i + 1] :
return True
return False
Try creating a True False function for each value check operation you want done taking the list as a parameter. then call each function like "if 1 and 2 print 3" format. That may make thinking through the flow a little easier.
Is this kind of what you were wanting?
def isincreasing(values):
if values==sorted(values):
return True
return False
def has2adjdup(values):
for x in range(len(values)-1):
if values[x]==values[x+1]:
return True
return False
if isincreasing(values) and has2adjdup(values):
print "True"