Question

In: Computer Science

PYHTON A company uses codes to represent its products, for example ABC2475A5R-14. Valid codes must have:...

PYHTON

A company uses codes to represent its products, for example ABC2475A5R-14. Valid codes must have:

  • At least 10 characters (some codes have more than 10 characters)

  • Positions 4 through 7 must be digits (and represents the country in which the product will be sold)

  • The character in the 10th position must be a capital letter and represents the security level of the product.

Write a Python program that will read 10 product codes from a text file (Codes.txt - attached below). Determine if each conforms to the rules listed above:

If it does conform, print a heading: Valid Code(s) are: and list the codes that are valid. If it does not conform, print a heading: Invalid Code(s) are: and list the codes that area invalid. Be sure to test all possible combinations.

Further, if the code is valid, due to new government security laws, products with a security level of R (restricted) are no longer to be sold in countries with a country code of 2000 or higher. Output a heading: Invalid Restricted Code(s) are: and list the codes that are invalid in case you encounter any products that violate these new laws.

.txt file

XYZ2755R-14
RST1234A6A-12
UVW24a6R7R-13
PQR3999F85-11
STI1281J9A-04
FOR2561T4R-54
BID2075U3R-55
AGA1475P1B01
JBT2175E5X-04
KAM1145X2R-05

PYTHON

Solutions

Expert Solution

Code.py

#importing regex library as I have used regex in my code for matching

import re

#three list for storing different types of codes

valid=[]

invalid=[]

restricted=[]

#opening a txt file in read mode

f = open("Codes.txt", "r")

for x in f:

str=x;

s1 = str[3:7] #Cutoff the country code part of string from the given code and take in s1 string for further matching

if (len(str)<10): # first matching that code is of more than or equal to of length 10 or not

invalid.append(str) # if not add it to invalid list

elif (not(re.search("^[0-9]+$", s1))): #if length is ok then check all country codes are numeric or not

invalid.append(str) # if not add it to invalid

elif (not(re.search("^[A-Z]+$", str[9]))): #similary checking 10th character is capital or not

invalid.append(str) # if not add it to invalid

else:

valid.append(str) # else it is a valid code

# if valid then check Security code is R or not if yes add it to restricted list by checking its country code

if (str[9]=="R" and int(s1)>2000):

restricted.append(str)

f.close() # close the file

#print the result

print("\nValid Code(s) are:\n")

for a in valid:

print(a)

print("\nInvalid Code(s) are:\n")

for a in invalid:

print(a)

print("\nInvalid Restricted Code(s) are:\n")

for a in restricted:

print(a)

Codes.txt

XYZ2755R-14
RST1234A6A-12
UVW24a6R7R-13
PQR3999F85-11
STI1281J9A-04
FOR2561T4R-54
BID2075U3R-55
AGA1475P1B01
JBT2175E5X-04
KAM1145X2R-05

Snaps of Code and Output:


Related Solutions

Describe an example of a company that markets its products or services to a specific target...
Describe an example of a company that markets its products or services to a specific target market (do NOT use a mass marketed product such as Coca Cola or bottled water) Describe which of the four major segmenting variables was used to determine this market. Is this a good, viable market for this product? Why or why not?
A contract is a set of legally enforceable promises. A valid contract must have agreement/ offer...
A contract is a set of legally enforceable promises. A valid contract must have agreement/ offer consideration, legal object/purpose, and parties with legal capacity. Sometimes two parties will enter into a contract with the intent to benefit a third party. In these situations, a third-party beneficiary contract is formed. Third-party contracts are difficult to follow with the rights and obligations of the parties. You are required to create a contract (keep it simple-one page). You will select the subject of...
Lavender Company produces several products in its factory, including a karate robe. The company uses a...
Lavender Company produces several products in its factory, including a karate robe. The company uses a standard cost system. According to the standards that have been set for the robes, the factory should work 780 direct labor hours eac month and produce 1,950 robes. Certain standard costs associated with this level of production are as follows: Direct labor $7,020 $3.60 Variable manufacturing overhead (based on DLH) $2,340 $1.20 During April, the factory worked 760 direct labor hours and produced 2,000...
The Petroco Company uses a highly toxic chemical in one of its manufacturing processes. It must...
The Petroco Company uses a highly toxic chemical in one of its manufacturing processes. It must have the product delivered by special cargo trucks designed for safe shipment of chemicals. As such, ordering (and delivery) costs are relatively high, at $2,600 per order. The chemical product is packaged in 1-gallon plastic containers. The cost of holding the chemical in storage is $50 per gallon per year. The annual demand for the chemical, which is constant over time, is 2,000 gallons...
What is the output of the following codes, consider each case independently. I have an example...
What is the output of the following codes, consider each case independently. I have an example of Case 1 to show you how to complete each case at the end of this Module. Case 1: What will be the value of x after the following loop is executed: x = 0 For i = 1 To 2 For j = 1 To 3      x = x * i * j Next j Next i Case 2: What will be...
According to the loop invariant theorem, a valid loop invariant ?(?)I(n) must have the property that...
According to the loop invariant theorem, a valid loop invariant ?(?)I(n) must have the property that Select one: a. ?(?)=0I(n)=0 after a finite number of steps. b. After the loop, ?ā†’?(?)Pā†’I(N) where ?P is the postcondition. c. ?(0)ā†’?I(0)ā†’Q, where ?Q is the precondition. d. None of the other answers are correct
A company uses activity-based costing to determine the costs of its three products: A, B, and...
A company uses activity-based costing to determine the costs of its three products: A, B, and C. The budgeted cost and activity for each of the company's three activity cost pools are shown in the following table: Budgeted Activity Activity Cost Pool Budgeted Cost Product A Product B Product C Activity 1 $ 85,000 7,500 10,500 21,500 Activity 2 $ 60,000 8,500 16,500 9,500 Activity 3 $ 112,000 4,000 2,500 3,125 How much overhead will be assigned to Product B...
A company uses activity-based costing to determine the costs of its three products: A, B, and...
A company uses activity-based costing to determine the costs of its three products: A, B, and C. The budgeted cost and activity for each of the company's three activity cost pools are shown in the following table: A company uses activity-based costing to determine the costs of its three products: A, B, and C. The budgeted cost and activity for each of the company's three activity cost pools are shown in the following table: Budgeted Activity Activity Cost Pool Budgeted...
A company uses activity-based costing to determine the costs of its three products: A, B, and...
A company uses activity-based costing to determine the costs of its three products: A, B, and C. The budgeted cost and activity for each of the company's three activity cost pools are shown in the following table: Budgeted Activity Activity Cost Pool Budgeted Cost Product A Product B Product C Activity 1 $ 75,000 6,500 9,500 20,500 Activity 2 $ 50,000 7,500 15,500 8,500 Activity 3 $ 92,000 3,000 1,500 2,125 How much overhead will be assigned to Product B...
CASE 14-2 Talbot Razor Products Company One of the products marketed by Talbot Razor Products Company...
CASE 14-2 Talbot Razor Products Company One of the products marketed by Talbot Razor Products Company is an after-shave lotion called Enhance. This brand is sold through drugstores, supermarkets, and department stores. Sales exceed $30 million per year but are barely profitable because of advertising expenses that exceed $9 million. For some time the company and its advertising agency have felt the need to undertake a study to obtain more data on the characteristics of their users as contrasted to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT