-2
  1. The first digit must be a 4.
  2. The fourth digit must be one greater than the fifth digit; keep in mind that these are separated by a dash since the format is ####-####-####.
  3. The sum of all digits must be evenly divisible by 4.
  4. If you treat the first two digits as a two-digit number, and the seventh and eighth digits as a two-digit number, their sum must be 100.
def verify(number) : 
  
   if input [0] == '4':
     return True
   if input [0] != '4':
     return "violates rule #1"


input = "5000-0000-0000" 
output = verify(input) 
print(output)
jmunsch
  • 22,771
  • 11
  • 93
  • 114
  • 1
    do you have a more specific question about the last 3 rules you're trying to write? what exactly are you having trouble with? assignment, indexing, division, lists, summing? also see: [ask] and [tour] – jmunsch Jul 31 '20 at 22:52
  • Im trying to write a code to test if the input is true or false. I'm having trouble coming up with the code to write the last 3 rules – Raul Emiliano Gonzalez Jul 31 '20 at 23:02
  • do you have anything more specific about the last 3 rules? what sort of trouble are you having? what have you tried for 2, 3, and 4? – jmunsch Jul 31 '20 at 23:07
  • for the second rule I don't know how to write the code to compare them with the dash in between. – Raul Emiliano Gonzalez Jul 31 '20 at 23:14
  • 1
    I’d strongly recommend using another variable name than `input`, as this is overwriting the built-in function. – S3DEV Jul 31 '20 at 23:34

2 Answers2

0

ord(x) is a function that takes a char, in this case x, and returns it's ascii value. That means, that ord('0') will return 48, ord('1') returns 49 and ord('2') returns 50.

So for the second rule, you can simply do:

if ord(input[3])+1 == ord(input[5])
    return True

For the third rule you could do

sum = 0
for x in range(14):
    if (input[x] != '-')
        sum += ord(input[x])-48

You need to subtract 48 from ord(input[x]) because, well, the ascii value of '0' is 48, '1' is 49 etc. Finally you return true, if sum is divisible by 4.

if (sum % 4 == 0)
    return True 

Now for the final rule, you simply get the values of the four respective digits and return true if their sum is 100.

sumOneTwo = (ord(input[0])-48)*10 + (ord(input[1])-48)
sumSevenEight = (ord(input[7])-48)*10 + (ord(input[8]-48)
if ((sumOneTwo + sumSevenEight) == 100)
    return True
0

Here is a version that doesn't use if statements:

def verifya(number):
    '''
      It helps to break each part down
      Say it out loud several times
      Think about what it requires
      write out some psuedo code
    '''
    try:
        # https://stackoverflow.com/questions/743806/how-to-split-a-string-into-a-list
        assert len(number.split('-')) == 3 \
               and all([len(x) == 4
                        for x in number.split('-')])
        # The first digit must be a 4.
        # https://stackoverflow.com/questions/32839561/python-how-to-get-first-item-in-list-on-list-in-python3
        assert number[0] == '4'
        # The fourth digit must be one
        # greater than the fifth digit;
        g1, g2, g3 = number \
            .split('-')  # split up the string on the
        # dashes so we get 5000, 0000, 0000
        ## keep in mind that these are # separated by a dash since the format is ####-####-####.
        assert ((int(g1[-1] # the fourth number
                     ) -\
                 int(
                    g2[0]  # and
                    # subtract the 5th
                )
                    # it should be one greater
                ) == 1)
        # The sum of all digits must be evenly divisible by 4.
        assert (sum([int(x) for x in y for y in [g1, g2, g3]]) % 4) == 0  # sum all characters and modulo by 4
        assert ((int(
            # If you treat the first two digits as a two-digit number
            g1[:2]) +\
                 int(
                    # and the seventh and eighth digits as a two-digit number
                    g2[-2:])) 
        #  their sum must be 100.
        == 100)  # https://stackoverflow.com/questions/509211/understanding-slice-notation
    except:
        return type('Homework', (), {"is": False})
    return type('Homework', (), {"is": True})

You can then run it with:

import pdb
pdb.set_trace()
import sys;print(getattr(verify("5000-0000-0000"), 'is'), file=sys.stdout)
jmunsch
  • 22,771
  • 11
  • 93
  • 114