Modulo is not processing correctly - python

I cannot seem to make sure that the python modulo function is working properly, I have tried various numbers and cannot seem to get a correct division.
ISBN Number
print """
Welcome to the ISBN checker,
To use this program you will enter a 10 digit number to be converted to an International Standard Book Number
"""
ISBNNo = raw_input("please enter a ten digit number of choice")
counter = 11 #Set the counter to 11 as we multiply by 11 first
acc = 0 #Set the accumulator to 0
Begin the loop, multiply each digit in the string by a decrimenting counter
We need to treat the number as a string to obtain each placement
for i in ISBNNo:
print str(i) + " * " + str(counter)
acc = acc + (int(i) * counter) #cast value a integer and multiply by counter
counter -= 1 #decrement counter
print "Total = " + str(acc)
Mod by 11 (divide and take remainder
acc = acc % 11
print "Mod by 11 = " + str(acc)
take it from 11
acc = 11 - acc
print "subtract the remainder from 9 = " + str(acc)
concatenate with string
ISBNNo = ISBNNo + str(acc)
print "ISBN Number including check digit is: " + ISBNNo

Your code is mostly correct, except for some issues:
1) You're trying to compute the checksum (last digit) if the ISBN. This means that you should only take 9 digits into consideration:
ISBNNo = raw_input("please enter a ten digit number of choice")
assert len(ISBNNo) == 10, "ten digit ISBN number is expected"
# ...
for i in ISBNNo[0:9]: # iterate only over positions 0..9
# ...
2) Also there should be a special case here:
ISBNNo = ISBNNo + str(acc)
print "ISBN Number including check digit is: " + ISBNNo
You're doing modulo-11, so acc can be equal to 10. ISBN mandates that "X" should be used as the last "digit" in this case, which could be written as:
ISBNNo = ISBNNo + (str(acc) if acc < 10 else 'X')
Here's the fixed code, with example number from Wikipedia: http://ideone.com/DaWl6y
In response to comments
>>> 255 // 11 # Floor division (rounded down)
23
>>> 255 - (255//11)*11 # Remainder (manually)
2
>>> 255 % 11 # Remainder (operator %)
2
(Note: I'm using // which stands for floor division. In Python 2, you could simply use / too because you're dividing integers. In Python 3, / is always true division and // is floor division.)

Related

sum of every other digit and doubling the rest

I'm working on a card number check code, for now I created a function that asks for the card number and checks if it is 8 digits or not (it has to be 8) and then calls another function that will do the math and check if the card is valid. For this function:
Starting from the rightmost digit, form the sum of every other digit. For example, if the card number is 1234 5678, then you form the sum 8 + 6 + 4 + 2 = 20
Double each of the digits that were not included in the preview step and then add all digits of the resulting numbers. For example, the digits that were not included are 7 5 3 1, we double the, 14 10 6 2, and then we sum each digit, 1 + 4 + 1 + 0 + 6 + 2 = 14
Add the sums of the two steps, 20 + 14 = 34, if the last digit is 0 then the card is valid, otherwise it is not valid (which is our case)
My problem is that I don't know how to iterate and get the sum of every other digit or double the other number which were not included in step 2. My thought was to use a while loop but then what?
EDIT: since some answers used lists... we didn't study lists yet, so I should not use it, we are only allowed to use sample stuff, loops, functions, etc.. even sum(map()) we didn't study
That is my code for now (its not much but just thought put it anyway)
def getCard():
CardInput = int(input("Enter your 8 digit credit card number: "))
if len(CardInput) == 8:
CardCheck(CardInput)
else:
print("Invalid Input: Should be exactly 8 digits!")
getCard()
def CardCheck(CardNumber):
Position = 0
Sum = 0
DoubleSum = 0
FinalSum = 0
while CardNumber >= 0:
Position += 1
So, the ugly way of doing is, you can write a for loop and use indexing for access specific elements
for i in range(len(CardInput)):
# it will iterate from 0 to 7
print(CardInput[i]) # here you get ith element
if i % 2 == 1:
print("I am other!") # you can sum your things here into another variable
Or with while:
while position < len(CardInput):
print(CardInput[position])
position += 1
It assumes CardInput is str, so I recommend to not convert it earlier.
However pythonic way would be
sum(map(int, CardInput[1::2])))
CardInput[1::2] returns list of every second element starting from second (0 is first).
map converts every element to in.
sum sums elements.
prompt = "Enter the eight-digit number: "
while True:
number = input(prompt)
if len(number) == 8 and number.isdigit():
break
prompt = "Oops, try again: "
first_digits = number[1::2] # If the user entered '12345678', this will be the substring '2468'
first_sum = sum(map(int, first_digits)) # Take each digit (character), map it to a single-digit integer, and take the sum of all single-digit integers
second_digits = number[0::2] # If the user entered '12345678', this will be the substring '1357'
doubled_ints = [int(char) * 2 for char in second_digits] # Take each digit (character), turn it into an integer, double it, and put it in a list.
second_sum = sum(map(int, "".join(map(str, doubled_ints)))) # Merge all integers in 'doubled_ints' into a single string, take each character, map it to a single digit integer, and take the sum of all integers.
total_sum = first_sum + second_sum
total_sum_last_digit = str(total_sum)[-1]
is_valid_card = (total_sum_last_digit == '0')
if is_valid_card:
print("Your card is valid (total sum: {})".format(total_sum))
else:
print("Your card is NOT valid (total sum: {})".format(total_sum))
def getCard():
CardInput = input("Enter your 8 digit credit card number: ")
if len(CardInput) == 8:
CardCheck(CardInput)
else:
print("Invalid Input: Should be exactly 8 digits!")
getCard()
def CardCheck(CardNumber):
list_CardNumber = [x for x in "25424334"]
Sum = sum(int(x) for x in list_CardNumber[1:8:-2])
DoubleSum = 2*sum(int(x) for x in list_CardNumber[0:8:-2])
FinalSum = Sum + DoubleSum
if str(FinalSum)[-1] == "0":
print("Valid Input")
else:
print("Invalid Input")
To get you started, you should check out enumerate(), it'll simplify things if you're just going to use loops by giving you easy access to both the index and value every loop.
step1 = 0
for i, x in enumerate(number):
if i % 2:
print('index: '+ str(i), 'value: '+ x)
step1 += int(x)
print('step 1: ', step1)
Output:
index: 1 value: 2
index: 3 value: 4
index: 5 value: 6
index: 7 value: 8
step 1: 20
You can use:
# lets say
CardNumber = '12345678'
# as mentioned by kosciej16
# get every other digit starting from the second one
# convert them to integers and sum
part1 = sum(map(int, CardNumber[1::2]))
# get every other digit starting from the first one
# convert them to integers and double them
# join all the digits into a string then sum all the digits
part2 = sum(map(int,''.join(list(map(lambda x: str(int(x)*2), CardNumber[0::2])))))
result = part1 + part2
print(result)
Output:
34
Edit:
Only with loops you can use:
# lets say
CardNumber = '12345678'
total_sum = 0
for idx, digit in enumerate(CardNumber):
if idx % 2 == 1:
total_sum += int(digit)
else:
number = str(int(digit)*2)
for d in number:
total_sum += int(d)
print(total_sum)
output:
34
Since you need to iterate over the digits, it's actually easier IMO if you leave it as a string, rather than converting the input to an int; that way you can just iterate over the digits and convert them to int individuall to do math on them.
Given an 8-digit long string card, it might look like this, broken into steps:
even_sum = sum(int(n) for n in card[1::2])
double_odds = (2 * int(n) for n in card[0::2])
double_odd_sum = sum(int(c) for do in double_odds for c in str(do))
All together with some logic to loop while the input is invalid:
def get_card() -> str:
"""Returns a valid card number, or raises ValueError."""
card = input("Enter your 8 digit credit card number: ")
if len(card) != 8 or not card.isdecimal():
raise ValueError("Invalid input: Should be exactly 8 digits!")
card_check(card)
return card
def card_check(card: str) -> None:
"""Raises ValueError if card checksum fails, otherwise returns None."""
even_sum = sum(int(n) for n in card[1::2])
double_odds = (2 * int(n) for n in card[::2])
double_odd_sum = sum(int(c) for do in double_odds for c in str(do))
if (even_sum + double_odd_sum) % 10:
raise ValueError("Card checksum failed!")
while True:
try:
print(f"{get_card()} is a valid card number!")
except ValueError as e:
print(e)

how can I convert binary to decimal without using bin method

whenever I run my code I get a TypeError saying "not all arguements converted during string formatting" and I tried using str() around what didn't get converted but I ran into more errors.
Here is my code:
def decimalToBinary(num):
bits = " "
while(num > 0):
bits = str(num%2) + bits
num = num//2
return bits
def binaryToDecimal(bits):
deciNum = 0
powers = 0
for i in reversed(bits):
deciNum = 2 **powers** (bits % 10)
bits /= 10
powers += 1
return deciNum
#program tester
for i in range(135, 146):
x = decimalToBinary(i)
deciNum = binaryToDecimal(x)
print(str(decimal))+ ' is '+ ' in Binary.'
I get this TypeError on the line that says "deciNum = 2 ** powers ** (bits%10)
Try this:
def binaryToDecimal(b_num: str) -> int:
d_num = 0
for i in range(len(b_num)):
digit = b_num.pop()
if digit == '1':
d_num = d_num + 2**i
return d_num
Note that b_num is a string, not an int. So you need to use this function in this way binaryToDecimal('101') (and not in this way binaryToDecimal(101)).
To answer the title of the post and to keep the types consistent with your program:
import math
def binaryToDecimal(bits):
# Initialize integer for number.
num = 0
# For each bit, multiply by power of 2 corresponding to its position.
# Then, add that power of 2 to the total counter.
for i in range(len(bits)):
num += int(bits[i]) * (2 ** (len(bits)-i-1))
# Return integer type.
return str(num)
def decimalToBinary(num):
# Determine how many bits represent the decimal number.
num_of_bits = int(math.log(num, 2)) + 1
bits = ''
# Shift the number over 1 more place to the right in each iteration.
# Then test the sign of the bit with AND.
for i in reversed(range(num_of_bits)):
bits += str(int(1&(num>>i)))
return bits
for i in range(135, 146):
x = decimalToBinary(i)
deciNum = binaryToDecimal(x)
print(str(deciNum)+ ' is '+ str(x)+' in Binary.')

need help in understanding a code

Can anyone explain this code a little. I can't understand what n does here? We already have taken N = int(input()) as input then why n=len(bin(N))-2? I couldn't figure it out.
N = int(input())
n = len(bin(N))-2
for i in range(1,N+1):
print(str(i).rjust(n) + " " + format(i,'o').rjust(n) + " " + format(i,'X').rjust(n) + " " + format(i,'b').rjust(n))
n counts the number of bits in the number N. bin() produces the binary representation (zeros and ones), as as string with the 0b prefix:
>>> bin(42)
'0b101010'
so len(bin(n)) takes the length of that output string, minus 2 to account for the prefix.
See the bin() documentation:
Convert an integer number to a binary string prefixed with “0b”.
The length is used to set the width of the columns (via str.rjust(), which adds spaces to the front of a string to create an output n characters wide). Knowing how many characters the widest binary representation needs is helpful here.
However, the same information can be gotten directly from the number, with the int.bitlength() method:
>>> N = 42
>>> N.bit_length()
6
>>> len(bin(N)) - 2
6
The other columns are also oversized for the numbers. You could instead calculate max widths for each column, and use str.format() or an f-string to do the formatting:
from math import log10
N = int(input())
decwidth = int(log10(N) + 1)
binwidth = N.bit_length()
hexwidth = (binwidth - 1) // 4 + 1
octwidth = (binwidth - 1) // 3 + 1
for i in range(1, N + 1):
print(f'{i:>{decwidth}d} {i:>{octwidth}o} {i:>{hexwidth}X} {i:>{binwidth}b}')

Python3 check digit algorithm

I'm learning to wrap my head around programming and have been given the following task:
The ISBN (International Standard Book Number) is made out of 10 digits.
z1z2z3z4z5z6z7z8z9z10
The last digit z10 is a check-digit. It's made like this: First, you create a kind of cross-sum with this formula:
s = 1 * z1 + 2 * z2 + 3 * z3 + 4 * z4 + 5 * z5 + 6 * z6 + 7 * z7 + 8 * z8 + 9 * z9
The check-digit z10 is the remainder of the integer division of s divided by 11. For the remainder 10 you write x or X. Example: For the ISBN 3826604237 you get the check-digit 7.
Calculation: 1*3+2*8+3*2+4*6+5*6+6*0+7*4+8*2+9*3 = 150
The remainder of the division of 150 and 11 is 7.
The code-solution given is as followed:
# isbn.py
number = int(input("Please enter a 9-digit number: "))
z9 = number % 10
number = number//10
z8 = number % 10
number = number//10
z7 = number % 10
number = number//10
z6 = number % 10
number = number//10
z5 = number % 10
number = number//10
z4 = number % 10
number = number//10
z3 = number % 10
number = number//10
z2 = number % 10
number = number//10
z1 = number
sum = z1+2*z2+3*z3+4*z4+5*z5+6*z6+7*z7+8*z8+9*z9
checkdigit = sum%11
print("\nCheckdigit:", checkdigit)
My question simply is: How does it work? Why do I have to calculate "number // 10" and "number % 10" and this all the time? Is there a name for this kind of algorithm, and if so, how is it called?
I'd appreciate any kind of answer for this and if it seems like the easiest thing for you and you feel like I'm wasting your time, I'm sorry. So far I understood pretty much anything I've learned thus far learning python, but this task seemed a bit hard (it was in a very early chapter of the book I'm studying on work) and I got stuck and didn't get this out of my head.
Thank you in advance and have a nice day!
The operation x % 10 is called 'modulus' and returns the remainder of the division by 10. You use it in your code to isolate the rightmost digit.
The next operation x // 10 is called 'integer division', that is, a division which returns integers only (the fractional part (if any) is cut off). Integer division by 10 on a decimal number corresponds to a rightshift by one digit so that the next digit is shifted into the rightmost place.
You repeat these 2 steps until the last digit is isolated. Then you perform the multiplications, and finally take the modulus of 11 (the remainder of the division by 11) to obtain the check digit.
This repetitive code cries for a loop. Just imagine you had to handle 100 digit numbers.
You are using % aka modulus and integer division // to get one digit a time.
It is easier to not convert the whole number into an integer and then extract the individual digits, but to process the inputted string character wise.
Throw in some input validation and you get:
while True:
# don't convert to int
# repeat until exactly 9 digits are given
number = input("Please enter a 9-digit number: ").strip()
if number.isdigit() and len(number) == 9:
break
# generator method - enumerate gives you the position and the value of each character
# i.e. for enumerate('123') you get (0,'1') then (1,'2') then (2,'3')
# the sum function adds up each given tuple but premultiplies the value with its (pos+1) as position inside strings start at 0 for the 1st character - it also
# converts each single character to its integer value
s1 = sum( (pos+1)*int(num) for pos,num in enumerate(number))
# s1 is a summed generator expression for this:
s2 = 0 # do not use sum - its a built-in functions name
for pos,num in enumerate(number):
s2 += (pos+1)*int(num)
print(s1,s2) # both are the same ;o)
checkdigit = s1%11
print("\nCheckdigit:", checkdigit)
For 382660423 you get:
150 150
Checkdigit: 7
It's began from modulo arytmetic. And module, length of ICBN and coefficients is just agreement, cause coefficients has no matter (by modulo arytmetic properties (if x mod y = 0, than k * x mod y = 0, where k is integer)).

Split a char or an int :: Python 3

I am doing a self-paced course, CS50 on EDX platform, the goal is to ask a user for a credit card number and then validate if it's a valid Amex, Master or Visa card.
I'm stuck trying split a number greater than 10 (formatted as a String) so that the result is for example "14" = "1" and "4" the idea is that both numbers will be appended to a list[].
I am trying to implement a credit card checksum check based on the algorithm invented by Hans Peter Luhn, I am stuck in the second part of the algorithm (written as a comment on the code) because as the algorithm specifies I have to split 14 into 1 4 and THEN do the sum.
Credit card for test: 378282246310005
So far i have this result: 14 + 4 + 4 + 8 + 6 + 0 + 0 = 36 //Bad
and what i need to keep going is: 1 + 4 + 4 + 4 + 8 + 6 + 0 + 0 = 27 //Good
Here is my code:
def main():
cCNumber = str(input("Ingrese el numero de la tarjeta de credito: "))
if cCNumber[0] == "3" and (cCNumber[1] == "4" or cCNumber[1] == "7") :
numbers = listInts(cCNumber)
listSumador = checkSum(numbers)
listSumadorStr = str(listSumador)
print(listSumadorStr)
print("Amex")
#Convert string to a list of Ints.
def listInts(stRnumber):
listNumbers = []
for i in range(0,len(stRnumber),1):
listNumbers.append(int(stRnumber[i]))
return listNumbers
#Implement the following algorithm:
#1: Multiply every other digit by 2, starting with the number’s second-to-last digit, and then add those products' digits together.
#2: Add the sum to the sum of the digits that weren’t multiplied by 2.
#3: if the total’s last digit is 0 (or, put more formally, if the total modulo 10 is congruent to 0), the number is valid!
def checkSum(numbers):
sumador = []
for i in range(1,len(numbers),2):
provisoryList = []
provisoryList.append(int(numbers[i]) * 2)
sumador.append(int(numbers[i]) * 2)
return sumador
if __name__ == "__main__":
main()

Categories

Resources