Search This Blog

Showing posts with label Python-Debugging. Show all posts
Showing posts with label Python-Debugging. Show all posts

Techgig Geek Goddess Screening Round Solution - Python

 



Oddia and Evenia are two friends who love strings and prime numbers. Although they have the same taste and like similar things, they are enemies when it comes to even and odd numbers. Oddia likes the odd numbers and Evenia likes the even numbers. They have a problem for you to solve.

A string S of lowercase letters will be provided and you have to figure out if the given string is Prime String or not. The index starts at 1.

Prime String: A string is considered a prime string only if the absolute difference between the sum of odd indexed letters and even indexed letters is completely divisible by any of the odd prime numbers less than 10.

Note: For calculations, consider the ASCII value of lowercase letters.

Example:

String, S = abcdef

Summation of Odd Indexed letters, O = a + c + e = 97 + 99 + 101 = 297

Summation of Even Indexed letters, E = b + d + f = 98 + 100 + 102 = 300

Absolute Difference = |O-E| = |297-300| = 3

This is completely divisible by 3 and leaves 0 as remainder. Thus, the given string is a Prime String.

If the string is prime string, print Prime String otherwise print Causal String. Can you solve it?


Input Format

The first line of input consists of the number of test cases, T

Next N lines each consist of a string, S.

Note: Read the input from the console.

Constraints

1<= T <=10

2<= |S| <=10000

|S| is the length of the string.


Output Format

For each test case, print Prime String if the string is prime string otherwise print Casual String.


Sample TestCase 1

Input

2

bbae

abcdef

Output

Casual String

Prime String

Explanation

Test Case 1: 


Sum of Odd indexed letters, O = 98+97 = 195

Sum of Even indexed letters, E = 98 + 101 = 199

Absolute Difference = |195-199| = 4

This is not divisible by any of the odd prime numbers. The given string is a Casual String.

----------------------------------------

Solution:

def check():

    
    num = int(input())

 

    for i in range(num):
        st = input()
        i = 0
        even_num = 0
        odd_num = 0
        for char in st:
            if i % 2 == 0:
                even_num+=ord(char)
            else:
                odd_num+=ord(char)
            i+=1
            
        ab_diff = abs(even-odd)
    
        if ab_diff % 3 == 0 or ab_diff % 5 == 0 or ab_diff % 7 == 0:
            print("Prime String")
        else:
            print("Casual String")

check()



Object Oriented Coffee Maker Machine : Python

 

Object Oriented Coffee Maker Machine : Python
Object Oriented Coffee 
Maker Machine
( Python Based ) 

The functionality given to the virtual coffee machine is a replica of our actual coffee machine. But, this oops-oriented coffee machine can only take order one out of latte/espresso/cappuccino.

The code is designed after determining 5 conditions to serve the coffee:

  • Turn On & Off the machine
  • Print the available quantity of the resources & money
  • Process the made payment by the user
  • Here, check out the given payment is ok?
  • Serve the coffee
Before solving the above-mentioned conditions, the modules containing these functionalities are imported. And the functionality of these modules can be understood from below given class's documentation:

MenuItem Class

Attributes:

        

  • name

(str) The name of the drink.

e.g. “latte”

  • cost

(float) The price of the drink.

e.g 1.5

  • ingredients

(dictionary) The ingredients and amounts required to make the drink.

e.g. {“water”: 100, “coffee”: 16}

Menu Class

Methods:

  • get_items()

Returns all the names of the available menu items as a concatenated string.

e.g. “latte/espresso/cappuccino”

  • find_drink(order_name)

Parameter order_name: (str) The name of the drinks order.

Searches the menu for a particular drink by name. Returns a MenuItem object if it exists, otherwise returns None.

CoffeeMaker Class

Methods:

  • report()

Prints a report of all resources.

e.g.

Water: 300ml

Milk: 200ml

Coffee: 100g

  • is_resource_sufficient(drink)

Parameter drink: (MenuItem) The MenuItem object to make.

Returns True when the drink order can be made, False if ingredients are insufficient.

e.g.

True

  • make_coffee(order)

Parameter order: (MenuItem) The MenuItem object to make.

Deducts the required ingredients from the resources.

MoneyMachine Class

Methods:

  • report()

Prints the current profit

e.g.

Money: $0

  • make_payment(cost)

Parameter cost: (float) The cost of the drink.

Returns True when payment is accepted, or False if insufficient.

e.g. False


Final Code Structure

1. Turn On & Off the machine

is_on=True

while is_on:
options = menu.get_items()
choice = input(f"What would you like? ({options}):").lower()

if choice =='off':
is_on =
False
2. Print the available quantity of the resources & money - The final report

from menu import Menu, MenuItem
from coffee_maker import CoffeeMaker
from money_machine import MoneyMachine


coffee = CoffeeMaker()
money = MoneyMachine()
menu = Menu()

is_on = True

while is_on:
options = menu.get_items()
choice = input(f"What would you like? ({options}):").lower()

if choice =='off':
is_on = False
elif choice == 'report':
money.report()
coffee.report()
3. & 4. Process the made payment by the user. Also, check out the given payment is ok?

from menu import Menu, MenuItem
from coffee_maker import CoffeeMaker
from money_machine import MoneyMachine


coffee = CoffeeMaker()
money = MoneyMachine()
# menuitem = MenuItem()
menu = Menu()
is_on=True

while is_on:
options = menu.get_items()
choice = input(f"What would you like? ({options}):").lower()

if choice =='off':
is_on = False
elif choice == 'report':
money.report()
coffee.report()
else:
drink = menu.find_drink(choice)
if coffee.is_resource_sufficient(drink):
if money.make_payment(drink.cost):
5. Serve the coffee

from menu import Menu, MenuItem
from coffee_maker import CoffeeMaker
from money_machine import MoneyMachine


coffee = CoffeeMaker()
money = MoneyMachine()
menu = Menu()

is_on = True

while is_on:
options = menu.get_items()
choice = input(f"What would you like? ({options}):").lower()

if choice =='off':
is_on = False
elif choice == 'report':
money.report()
coffee.report()
else:
drink = menu.find_drink(choice)
if coffee.is_resource_sufficient(drink):
if money.make_payment(drink.cost):
coffee.make_coffee(drink)
Thank You :)

Solution to Exploring Data Types TechGig - 02


Exploring Data Types TechGig

Exploring Data Types TechGig


The question asked to check out the data type of the user input. It was quite tricky as python by default converts the type of the input into type str. So, I was halfway blank like what approach to use.


Further, I googled & ended up with this article: How You Make Sure input() Is the Type You Want It to Be in Python Unluckily, it introduced me with nothing new concept but, gave me a path to go & explore libraries. And while looking out for them I discovered regex that is regular expressions in python and to this, I thought of adding exception handling.

Finally, trial & test lead me to a code that actually passed out all the test cases. Seriously, Exception handling is a wingman.

Also, keeping below mentioned five custom inputs in mind, I had been able to see the clear picture:

Custom Input 1: Hello Engineer Bae
Expected Output: This input is of type string.

Custom Input 2: 22.33
Expected Output: This input is of type Float.

Custom Input 3: 33
Expected Output: This input is of type Integer.

Custom Input 4: @@
Expected Output: This is something else.

Custom Input 5:             #space as input since, space is a character
Expected Output: This input is of type string.

Here we go. The code that is the answer to the exploring data types - day 02 question of techgig

python hello.py File "", line 1 python hello.py ^ SyntaxError: invalid syntax

 Error: python hello.py File "<stdin>", line 1 python hello.py ^ SyntaxError: invalid syntax while running a python script in command prompt ( windows )


Possible Reasons: 

  1.  The problem is that you are trying to run python hello.py from within the Python interpreter, which is why you're seeing the traceback.
  2. You are not using the command prompt in administrator access.
  3. You are not in the right directory where your python files exist.

Solution: ( OS: Windows 10 )

Step 1: If you are using Windows, press Win+R and type cmd. As shown in the image below. And run it as administrator.


Step 2: Suppose in your computer, hello.py file is at this address: c:\users\yourname\desktop\pythonfiles

So, use the below-mentioned commands to go at the above-mentioned path.

C:\WINDOWS\system> cd..

C:\Windows>cd..

C:\Users>cd users\yourname\Desktop\

C:\Users\engineerbae\Desktop> cd python files

If you're already on the correct path directly jump to step 3.

Step 3: Now, run the hello.py this way: python hello.py in the command prompt.

You shall receive your output :)


Lately, when I ran my first python script using the command prompt. I had been using an interpreter directly and had no clue what's going wrong. 


Hope it helped.

Thank You!