Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Imagine you are working for the most amazing software company , whose most valuable product is an E-Commerce application. You have now been assigned to develop order section of the app that can generate an ORDER_NUMBER for a particular order and a BAR_CODE that needs to be stuck to the shipment package.

 

Steps to evaluate ORDER_NUMBER & BAR_CODE are given below.

 

Inputs will consist of:-

Array size and array of available products in the warehouse | array of alphabetical string only (say product names);
Array size and array of products added to cart | Array of strings;
 

Output :-

 Consist of two lines where first line represents the ORDER_NUMBER and second line represents BAR_CODE.
  ORDER_NUMBER
  BAR_CODE
 

Inputs :

7
tshirt
cargos
Sweatshirt
covers
handBags
denims
cleaner
6
tshirt
10-cargos
#sweatSHirt
Denims
chips
cleane

 

Output :

99998

|| | ||| || |||

 

Where :-

1st line represents the ORDER_NUMBER

2nd line represents the BAR_CODE

 

EXPLANATION 

 

Let :-

all_products = ["tshirt", "cargos", "Sweatshirt", "covers", "handBags", "denims", "cleaner"]

cart = ["tshirt", "10-cargos", "#sweatSHirt", "Denims", "chips", "cleaner"]

 

 

Steps to evaluate the result:

 

Step 1 : Data pre-processing
Eliminate any character other than alphabets from every element in cart and convert them to lowercase.
pre_processed_cart = ["tshirt", "cargos", "sweatshirt", "denims", "chips", "cleaner"];
Eliminate any item which is not present in "all_products" from "pre_processed_cart" (NOTE : The comparision is not case senstive).
clean_cart = ["tshirt", "cargos", "sweatshirt", "denims", "cleaner"];
Step 2 : Generate ORDER_NUMER
Combine all elements of clean_cart to make a cart string
cart_str = "tshirtcargossweatshirtdenimscleaner";
Find the first 2 longest substring out of the "cart_str" without repeating character.
lng-str1 = "rtdenimscl"
lng-str2 = "tdenimscl"
Add up both sub-string.
concat_str = "rtdenimscl"+"tdenimscl = "rtdenimscltdenimscl"
Replace each character in concat_str with the index of occurance on alphabatical line (index starting from 1) like replace : a --> 1 , b --> 2, c --> 3 and so on.
concat_num_str = "182045149131931220451491319312"
Take at most 5 greatest single digit integer from "concat_num_str" and concat the 5 characters to get the order number.
order_number = "99998"
Step 3 : Generate BAR_CODE
Sort the clean_cart in alphabetical order and take first five elements.
sliced_cart = ["cargos", "cleaner", "denims", "sweatshirt", "tshirt"];
Replace element in the "sliced_cart" with the sum of index of occurance value (on alphabetical line, index starting from 0, Like replace : a --> 0, b --> 1 ....) of each character of element. Then take modular with 5. e.g -> "cargos" = 2+0+17+6+14+18 = 57.
sliced_cart_new = [57,51,58,132,88];
// #NOTE if the modular value is 0 consider it 1;
modular_cart = [2, 1, 3, 2, 3];
Now make a barcode string with the number of strides "|" for every element in "modular_cart" separated by spaces
bar_code = "|| | ||| || |||"
Step 4 : Return the final result
Two lines where : 1st line represents the ORDER_NUMBER  and 2nd line represents the BAR_CODE. 
99998

|| | ||| || |||

 

# Note for any wrong or empty inputs take order_number="0" , bar_code="".


What I have tried:

def process_order(all_products, cart):
    # Step 1: Data pre-processing
    pre_processed_cart = [product.lower() for product in cart if any(product.lower() == p.lower() for p in all_products)]

    # Step 2: Generate ORDER_NUMBER
    cart_str = ''.join(pre_processed_cart)

    def find_longest_substring(s):
        longest = ''
        current = ''
        for char in s:
            if char not in current:
                current += char
                if len(current) > len(longest):
                    longest = current
            else:
                current = current[current.index(char) + 1:] + char
        return longest

    lng_str1 = find_longest_substring(cart_str)
    cart_str = cart_str.replace(lng_str1, '', 1)
    lng_str2 = find_longest_substring(cart_str)
    concat_str = lng_str1 + lng_str2

    def char_to_index(char):
        return str(ord(char.lower()) - ord('a') + 1)

    concat_num_str = ''.join([char_to_index(char) for char in concat_str])
    order_number = ''.join(sorted(concat_num_str, reverse=True)[:5])

    # Step 3: Generate BAR_CODE
    sorted_cart = sorted(pre_processed_cart)
    sliced_cart = sorted_cart[:5]

    def calculate_modular_value(product):
        value = sum(ord(char) - ord('a') for char in product.lower()) % 5
        return value if value > 0 else 1

    modular_cart = [calculate_modular_value(product) for product in sliced_cart]
    bar_code = ' '.join(['|' * value for value in modular_cart])

    # Step 4: Return the result
    return order_number, bar_code

# Example inputs
all_products = ["tshirt", "cargos", "Sweatshirt", "covers", "handBags", "denims", "cleaner"]
cart = ["tshirt", "10-cargos", "#sweatSHirt", "Denims", "chips", "cleaner"]

# Generate ORDER_NUMBER and BAR_CODE
order_number, bar_code = process_order(all_products, cart)

# Print the result
print(order_number)
print(bar_code)
Posted

1 solution

Just because it runs does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
private int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!

If you don't know how to use a debugger, then start here: pdb — The Python Debugger — Python 3.11.5 documentation[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900