![shashayantie6556](/avatars/21532.jpg)
shashayantie6556
10.12.2020 •
Computers and Technology
Python Exercise 4
1. In the nine-digit Social Insurance Number (SIN) given to each person having a job or
filing an income tax return in Canada, the ninth digit is a checked digit that is used to test
the validity of the other digits in the SIN. The ninth digit is determined by the following
procedure.
a. Double the 2 nd , 4 th , 6 th , and 8 th digits, if it will become a 2 digit number, add all the
digits to make it a single digit. For example, 6x2 =12, then 1+2=3
b. Add the digits of the number found in step (a)
c. Add the 1 st , 3 rd , 5 th , and 7 th digits.
d. Add the number found in steps (b) and (c)
e. Subtract the units digit of the result of step (d) from 10 and note the units digit of the
result. For the SIN to be valid, its ninth digit must have this value.
Write a program that repeatedly reads nine-digit numbers and determines whether or not
each number is a valid SIN> The program should stop when it reads the value
999999999.
Solved
Show answers
More tips
- S Science and Technology When do we change our clocks?...
- A Animals and plants How to Teach Your Parrot to Talk?...
- F Family and Home How to Remove Fading from Clothes: Tips and Tricks...
- F Food and Cooking How to Make Polendwitsa at Home?...
- F Family and Home Parents or Environment: Who Has the Most Influence on a Child s Upbringing?...
- P Philosophy Unbelievable stories of encounters with otherworldly forces...
- L Leisure and Entertainment How to Choose the Perfect Gift for Men on February 23rd?...
- H Health and Medicine How to Treat Whooping Cough in Children?...
- H Health and Medicine Simple Ways to Lower Cholesterol in the Blood: Tips and Tricks...
- O Other How to Choose the Best Answer to Your Question on The Grand Question ?...
Answers on questions: Computers and Technology
- E English What is the point of view of Abby s parents? Part I: Parents of Rescued Teenage Sailor Abby Sunderland Accused of Risking Her Life...
- M Mathematics Angles 7 & 8 are because they are...
- M Mathematics What label should you put on the mark between -2 and -3?...
- B Business Suppose general electric paid its line workers $12 per hour in 2015 when the consumer price index was 100. suppose that deflation occurred and the aggregate price level...
Ответ:
# stack counter
def sc(n):
sum = 0
for d in str(n):
sum += int(d)
if sum>9:
return sc(sum)
return sum
# double stack count
def dsc(n):
return sc(2*n)
def isValidSIN(s):
if len(s) != 9:
return False
sum = dsc(s[1]) + dsc(s[3]) + dsc(s[5]) + dsc(s[7])
sum += int(s[0]) + int(s[2]) + int(s[4]) + int(s[6])
checkDigit = (10 - (sum % 10))%10
return int(s[-1]) == checkDigit
SINS = [
'518759097',
'066600271',
'666062989',
'299675595',
'022147292',
'321122210',
'553277039',
'398148031',
'392430286',
'548504232',
'463665182',
'217427954',
'204305551',
'000071647']
for sin in SINS:
if isValidSIN(sin): print(sin + " is valid")
else: print(sin + " is INVALID")
Explanation:
I do believe there is a mistake in step e. If the sum has a units digit of 0, the subtraction would give you 10 for the check digit and that will fail. Therefore I added another modulo 10.
Ответ:
answer:
my computer
Explanation: