Question

Python 3.1 - 3.5 Certain machine parts are assigned serial numbers. Valid serial numbers are of...

Python 3.1 - 3.5

Certain machine parts are assigned serial numbers. Valid serial numbers are of the form: SN/nnnn-nnn where ‘n’ represents a digit. Write a function valid_sn that takes a string as a parameter and returns True if the serial number is valid and False otherwise.

For example, the input "SN/5467-231" returns True "SN5467-231" returns False

"SN/5467-2231" returns False

Using this function, write and test another function valid-sn-file that takes three file handles as input parameters. The first handle is to the file contains a list of serial numbers, the second and third are to output files. Write all valid serial numbers to the second file handle and invalid serial numbers to the third.

txt file:

Boston Americans
New York Giants
Chicago White Sox
Chicago Cubs
Chicago Cubs
Pittsburgh Pirates
Philadelphia Athletics
Philadelphia Athletics
Boston Red Sox
Philadelphia Athletics
Boston Braves
Boston Red Sox
Boston Red Sox
Chicago White Sox
Boston Red Sox
Cincinnati Reds
Cleveland Indians
New York Giants
New York Giants
New York Yankees
Washington Senators
Pittsburgh Pirates
St. Louis Cardinals
New York Yankees
New York Yankees
Philadelphia Athletics
Philadelphia Athletics
St. Louis Cardinals
New York Yankees
New York Giants
St. Louis Cardinals
Detroit Tigers
New York Yankees
New York Yankees
New York Yankees
New York Yankees
Cincinnati Reds
New York Yankees
St. Louis Cardinals
New York Yankees
St. Louis Cardinals
Detroit Tigers
St. Louis Cardinals
New York Yankees
Cleveland Indians
New York Yankees
New York Yankees
New York Yankees
New York Yankees
New York Yankees
New York Giants
Brooklyn Dodgers
New York Yankees
Milwaukee Braves
New York Yankees
Los Angeles Dodgers
Pittsburgh Pirates
New York Yankees
New York Yankees
Los Angeles Dodgers
St. Louis Cardinals
Los Angeles Dodgers
Baltimore Orioles
St. Louis Cardinals
Detroit Tigers
New York Mets
Baltimore Orioles
Pittsburgh Pirates
Oakland Athletics
Oakland Athletics
Oakland Athletics
Cincinnati Reds
Cincinnati Reds
New York Yankees
New York Yankees
Pittsburgh Pirates
Philadelphia Phillies
Los Angeles Dodgers
St. Louis Cardinals
Baltimore Orioles
Detroit Tigers
Kansas City Royals
New York Mets
Minnesota Twins
Los Angeles Dodgers
Oakland Athletics
Cincinnati Reds
Minnesota Twins
Toronto Blue Jays
Toronto Blue Jays
Atlanta Braves
New York Yankees
Florida Marlins
New York Yankees
New York Yankees
New York Yankees
Arizona Diamondbacks
Anaheim Angels
Florida Marlins
Boston Red Sox
Chicago White Sox
St. Louis Cardinals
Boston Red Sox
Philadelphia Phillies

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python Code:

Function: valid_sn

def valid_sn(serialNo):
   """
       Function that validates the Serial Number of form SN/nnnn-nnn
   """
   # Validating Length
   if len(serialNo) != 11:
       return False;
      
   # Checking for String , / and - in correct positions  
   if serialNo[:2] != "SN" or serialNo[2] != '/' or serialNo[7] != '-':
       return False;
  
   # Extracting sub-strings nnnn
   nnnn = serialNo[3:7];

   # Extracting sub-strings nnn
   nnn = serialNo[8:];
  
   # Checking that all characters in nnnn are digits
   for ch in nnnn:
       if not ch.isdigit():
           return False;
  
   # Checking that all characters in nnn are digits  
   for ch in nnn:
       if not ch.isdigit():
           return False;
  
   # All conditions satisfied, return True
   return True;
   

Sample Run:

Python 3.5.2 Shell File Edit Shell Debug Options Window Help 37 def valid sn(serialNo) Function that validates the Serial Num

_____________________________________________________________________________________________

Function: valid_sn_file

def valid_sn_file(ipFile, validSerialNoFile, invalidSerialNoFile):
   """
       First handle is to the file contains a list of serial numbers, the second and third are to output files
       Write all valid serial numbers to the second file handle and invalid serial numbers to the third
   """
  
   try:
       # Extracting data from file
       for serialNo in ipFile:
           # Stripping white spaces
           serialNo = serialNo.strip(" \n");
           # Validating serial number
           if valid_sn(serialNo):
               # Valid serial number - Writing to second file
               validSerialNoFile.write(serialNo);
               validSerialNoFile.write("\n");
           else:
               # InValid serial number - Writing to second file
               invalidSerialNoFile.write(serialNo);
               invalidSerialNoFile.write("\n");
      
       # Closing files
       ipFile.close();
       validSerialNoFile.close();
       invalidSerialNoFile.close();
      
       print("\n Data has been processed successfully.. Written to output files... \n");
      
   except Exception as ex:
       # Handling Exceptions
       print(ex);

Sample Run:

Python 3.5.2 Shell File Edit Shell Window serialNum.txt - Notepd File Edit Format View sss def valid an file(ipFile, validSer

Add a comment
Know the answer?
Add Answer to:
Python 3.1 - 3.5 Certain machine parts are assigned serial numbers. Valid serial numbers are of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PYTHON Text file Seriesworld attached below and it is a list of teams that won from...

    PYTHON Text file Seriesworld attached below and it is a list of teams that won from 1903-2018. First time mentioned won in 1903 and last team mentioned won in 2018. World series wasn’t played in 1904 and 1994 and the file has entries that shows it. Write a python program which will read this file and create 2 dictionaries. The first key of the dictionary should show name of the teams and each keys value that is associated is the...

  • Java : I keep getting something wrong in my loadfile method, how do I fix my...

    Java : I keep getting something wrong in my loadfile method, how do I fix my code? Thank you. here is what is contained in the text file (WorldSeriesWinners.txt) 112 1903 Boston Americans 1904 No World Series 1905 New York Giants 1906 Chicago White Sox 1907 Chicago Cubs 1908 Chicago Cubs 1909 Pittsburgh Pirates 1910 Philadelphia Athletics 1911 Philadelphia Athletics 1912 Boston Red Sox 1913 Philadelphia Athletics 1914 Boston Braves 1915 Boston Red Sox 1916 Boston Red Sox 1917 Chicago...

  • write a SQL statements to perform the following: Select all years a World Series game was...

    write a SQL statements to perform the following: Select all years a World Series game was played Select all losing teams that played in a World Series game Select all winning and losing teams that played in a World Series game Select all cities of a winning or losing team that played in a World Series game Select all winning and losing teams that played in a World Series game, and provide aliases of "Winning Team" and "Losing Team" Select...

  • To be done on excel: Team Revenue ($ millions) Value ($ millions) Arizona Diamondbacks 195 584...

    To be done on excel: Team Revenue ($ millions) Value ($ millions) Arizona Diamondbacks 195 584 Atlanta Braves 225 629 Baltimore Orioles 206 618 Boston Red Sox 336 1,312 Chicago Cubs 274 1,000 Chicago White Sox 216 692 Cincinnati Reds 202 546 Cleveland Indians 186 559 Colorado Rockies 199 537 Detroit Tigers 238 643 Houston Astros 196 626 Kansas City Royals 169 457 Los Angeles Angels of Anaheim 239 718 Los Angeles Dodgers 245 1,615 Miami Marlins 195 520 Milwaukee...

  • 3) American League baseball teams play their games with the designated hitter rule, meaning that pitchers...

    3) American League baseball teams play their games with the designated hitter rule, meaning that pitchers do not bat. The league believes that replacing the pitcher, typically a weak hitter, with another player in the batting order produces more runs. Using a significance level of a = 0.05, determine if the average number of runs is higher for the American League Following are the average number of runs scored by each team in the 2016 season: American League National League...

  • Tampa Texas Toront 4-30 In 2012, the total payroll for the New York Yankees was almost...

    Tampa Texas Toront 4-30 In 2012, the total payroll for the New York Yankees was almost $200 million, while the total payroll for the Oakland Athletics (a team known for using baseball analytics or sabermetrics) was about $55 million, less than one-third of the Yankees' payroll. In the following table, you will see the payrolls (in millions) and the total number of victories for the baseball teams in the American League in the 2012 season. Develop a regression model to...

  • 5. You and your friends want to select a few Major League Baseball (MLB) teams to...

    5. You and your friends want to select a few Major League Baseball (MLB) teams to create a fantasy league. You write a code to select teams at random. (Divisions and teams are listed below.) (12pts.) AL East AL West AL Central HAstros Orioles White Sox B C Red Sox Indians Angels Tigers Yankees Athletics KC Rays Royals Mariners T Twins Blue Jays Rangers NL East NL Central NL West C A Diamondbacks Braves Cubs Marlins Reds Rockies Mets Brewers...

  • Team League Wins ERA BA HR SB Errors Built Size Attendance Payroll Pittsburgh Pirates NL 57...

    Team League Wins ERA BA HR SB Errors Built Size Attendance Payroll Pittsburgh Pirates NL 57 5.00 0.242 126 87 127 2001 38496 1.61 34.9 San Diego Padres NL 90 3.39 0.246 132 124 72 2004 42445 2.13 37.8 Oakland Athletics AL 81 3.56 0.256 109 156 99 1966 34077 1.42 51.7 Texas Rangers AL 90 3.93 0.276 162 123 105 1994 49115 2.51 55.3 Florida Marlins NL 80 4.08 0.254 152 92 123 1987 36331 1.54 55.6 Arizona Diamondbacks...

  • The average number of minutes Americans commute to work is 27.7 minutes (Sterling's Best Places, April...

    The average number of minutes Americans commute to work is 27.7 minutes (Sterling's Best Places, April 13, 2012). The average commute time in minutes for 48 cities are as follows: Click on the datafile logo to reference the data. DATA file Albuquerque Atlanta Austin Baltimore Boston Charlotte Chicago Cincinnati Cleveland Columbus Dallas Denver Detroit El Paso Fresno Indianapolis 23.3 28.3 24.6 32.1 31.7 25.8 38.1 24.9 26.8 23.4 28.5 28.1 29.3 24.4 23.0 24.8 Jacksonville Kansas City Las Vegas Little...

  • Refer to the Lincolnville School District bus data. First, add a variable to change the type...

    Refer to the Lincolnville School District bus data. First, add a variable to change the type of engine (diesel or gasoline) to a qualitative variable. If the engine type is diesel, then set the qualitative variable to 0. If the engine type is gasoline, then set the qualitative variable to 1. Develop a regression equation using statistical software with maintenance cost as the dependent variable and age, odometer miles, miles since last maintenance, and engine type as the independent variables....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT