barcode_utilities.py

# Sum up the even place number if i2 0 assign each

• Homework Help
• 3

This preview shows pages 2–3. Sign up to view the full content.

#sum up the even place number if i%2 == 0: #assign each number with the set given on the website if s[i]=='0': eventotal +='3211' elif s[i]=='1': eventotal += '2221' elif s[i]=='2': eventotal += '2122' elif s[i]=='3': eventotal += '1411' elif s[i]=='4': eventotal += '1132' elif s[i]=='5': eventotal += '1231' elif s[i]=='6': eventotal += '1114' elif s[i]=='7': eventotal += '1312' elif s[i]=='8': eventotal += '1213' elif s[i]=='9': eventotal += '3112' else: #if the number is odd, do the same thing if s[i]=='0': eventotal +='3211' elif s[i]=='1': eventotal += '2221' elif s[i]=='2': eventotal += '2122' elif s[i]=='3': eventotal += '1411' elif s[i]=='4': eventotal += '1132' elif s[i]=='5': eventotal += '1231' elif s[i]=='6': eventotal += '1114' elif s[i]=='7': eventotal += '1312' elif s[i]=='8': eventotal += '1213' elif s[i]=='9': eventotal += '3112' #create a total *3 according to wiki total = oddunit*3 + eventotal #divide the total to add the middle section of 5 1's totallen=len(total) formattotal= '111' + total[0:int(totallen/2)]+'11111'+ total[int(totallen/2):totallen] + '111' # TODO: implement this function! return formattotal def valid_barcode(s): """Determines whether a barcode is valid or not based on length and the check digit. A "UPC-A" barcode consists of 12 digits, with the last digit being the check digit. Some examples: valid_barcode('036000291452') # --> True

This preview has intentionally blurred sections. Sign up to view the full version.

valid_barcode('036000291450') # --> False valid_barcode('075678164125') # --> True valid_barcode('') # --> False :param s: barcode number :type s: str :return: true if the barcode is valid, false otherwise :rtype: bool """ # TODO: implement this function! #check if the code is == 12 if len(s) == 12 and s.isdigit(): return True else: return False if __name__ == '__main__': print('Test your functions here!') print(generate_bar_widths('075678164125')) # (should be 11132111312123111141312121311111222111141132222121221231111) print(valid_barcode('075678164125')) # (should be valid!)
This is the end of the preview. Sign up to access the rest of the document.
• Spring '14
• Computer Science, Subroutine, Return statement, Universal Product Code, eventotal

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern