All posts All posts by this author Paper color Change page color Announcements

Elance Python2.x Test Problems and Solutions

Problem 1:

Write a program to calculate the bill amount, in cents, for the units
of power consumed. Following are the rest applicable:

  1. First 0-100 units: 60 cents per unit
  2. Next 200 units: 70 cents per unit
  3. Beyond 300 units: 80 cents per unit

The program should accept three different usage unit readings.

Example

If the following inputs are supplied:

305
180
120

Then the output should be:

20400
11600
7400

Sulution:

i = 0
inputs = []
while i < 3:
    inputs.append(int(raw_input("")))
    i += 1

for unit in inputs:
    bill = 0
    if unit > 300:
        bill = (unit - 300) * 80 + 200 * 70 + 100 * 60
    elif unit > 100 and unit <= 300:
        bill = (unit -100) * 70 + 100 * 60
    elif unit > 0 and unit <= 100:
        bill = unit * 60
    print bill

Problem 2:

Write a program which calculates and prints the Optional Order Quantity for the production
of LCD sets. Following is the formula to calculate Optional Order Quantity:

Q = Square root of [(2 * C * D) / H]

Following are the fixed values of C and H:

Fixed cost per order, denoted by C, is $50

Annual holiding cost per unit, denoted by H, is $30

Only the Annual Demand Quantity (D) of the product varies. The values of
D are supplied as input to your program in a comma separated sequence.

Example

Let us assume the following comma separated input sequence is given to the program:

100,150,180

The output of the program should be:

18,22,24

Solution:

D = raw_input("")
C = 50.0
H = 30.0
result = []

def get_order_quantity(D):
    return ((2 * C * D) / H) ** 0.5

for d in D.split(","):
    result.append(str(int(round(get_order_quantity(int(d))))))

print ",".join(result)

Problem 3:

Write a program which will receive a comma separated sequence of numbers.
Suppose a particular input number is represented by "n". The program should
find the greatest "n" digit number which is divisible by "n" itself. The
output should be comma seperated.

Example

If the following input is supplied to program:

2,5,8

Then the output should be:

98,99995,99999992

Solution:

ns = raw_input("")
result = []

def n_greatest_number(n):
    return int("9" * n)

for n in ns.split(","):
    number = n_greatest_number(int(n))
    mod = number % int(n)
    result.append(str(number - mod))

print ",".join(result)

Problem 4:

Write a program which accepts bank account transections in a comma separated
sequence and prints the net account balance. The various transactions are
classified as deposits (D) and withdrawals (W). Deposite transactions are
suffixed with a hyphen followed by D and Withdrawal transactions are
suffixed with a hyphen followed by W.

If the net account balance is more than 5000 dollars, an interest of 5% should be
added to the balance. The amount to be printed should be an integer vaalue, such
that if you are getting a output 4321.0, the program should give the output as 4321.

Example

If following input is supplied to the program:

2000-D,4000-D,500-W

Then the output of the program should be:

5775

Solution:

tran = raw_input("")
trans = tran.split(",")
bal = 0

for tr in trans:
    if tr[-1:] == "D":
        bal += int(tr[:-2])
    elif tr[-1:] == "W":
        bal -= int(tr[:-2])

if bal > 5000:
    bal = bal + (bal * 0.05)

print int(bal)

Problem 5:

Write a program which takes 4 inputs, where each input consists of 2 numbers
in the format x,y. You are required to print a two dimensional array having
x rows and y columns for each input. The elements of the arrays should be
whole numbers starting from 1 and incrementing by 1.

Example

Suppose the following 4 inputs are given to the program:

2,2
2,3
3,3
3,4

Then the output should be:

[[1, 2], [3, 4]]
[[1, 2, 3], [4, 5, 6]]
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]

Solution:

i = 0
inputs = []
while i < 4:
    inputs.append(raw_input(""))
    i += 1

for value in inputs:
    row = int(value.split(",")[0])
    col = int(value.split(",")[1])
    val = 1
    arr = []
    for r in xrange(row):
        arr.append([])
        for c in xrange(col):
            arr[r].append(val)
            val += 1
    print arr

Problem 6:

A bank has implemented criteria for determining whether the transaction
passwords typed by customers of the bank are valid or not.

Following are the criteria for checking the transaction password:

  1. At least 1 letter between [a-z]
  2. At least 1 letter between [0-9]
  3. At least 1 letter between [A-Z]
  4. At least 1 character from [*#+@]
  5. Minimum length of transaction password: 4
  6. Maximum length of transaction password: 6
  7. No space is allowed

Write a program which will accept a sequence of comma separated transaction
password and will check them according to the bank's criteria. Passwords
that match the criteria are to be printed, each separated by a comma.

Example

If the following passwords are given as input to the pprogram:

Abc@1,a B1#,2w3E*,2We#3345

Then the output of the program should be:

Abc@1,2w3E*

Solution:

import re

d = re.compile(r'[\d]+')
sl = re.compile(r'[a-z]+')
cl = re.compile(r'[A-Z]+')
sp = re.compile(r'[*#+@]+')
inputs = raw_input("")

result = []
for pas in inputs.split(","):
    if len(pas) >= 4 and len(pas) <= 6 and not pas.__contains__(" "):
        if d.search(pas) and sl.search(pas) and cl.search(pas) and sp.search(pas):
            result.append(pas)
print ",".join(result)

Problem 7:

Write a program which will convert the given RGB input values into the
corresponding hexadecimal values preceded with a # sign.

A single input sequence will br provided, consisting of RGB values separated
by "-". Each RGB combination will be separated by ",". The outputs should be
comma separated and in upercase. Also, it should be checked if a color value
is greater than 255. in such a case, the output for the corresponding
combination should be given as INVALID.

Note: Number 10, which has a hexadecimal value of A, must be represented as "0A"
and not as "A". The same rule applies to other single digit hexadecimal numbers.

Example

Suppose the following input sequence is supplied to the program:

12-13-14,45-156-23,234-234-256

The output of the program should be:

#0C0D0E,#2D9C17,INVALID

Solution:

raw = raw_input("")
result = []

for color in raw.split(","):
    value_list = color.split("-")
    color_hex = "#"
    for val in value_list:
        if int(val) >=0 and int(val) < 256:
            single_hex = hex(int(val))
            if len(single_hex[2:]) == 1:
                single_hex = '0' + single_hex[2:]
            else:
                single_hex = single_hex[2:]
            color_hex += single_hex.upper()
        else:
            single_hex = None
            continue
    if single_hex:
        result.append(color_hex)
    else:
        result.append("INVALID")
print ",".join(result)

Problem 8:

Write a program which will find all such numbers which are divisible by 7 but
are not multiple of 5, between 1000 and 1200 (both included). The numbers
obtained should be printed in a comma separated sequence on a single line.

Solution

result = []
for i in range(1000, 1201):
    if i % 7 == 0 and i % 5 !=0:
        result.append(str(i))

print ",".join(result)

Problem 9:

Write a program that accepts a comma separated sequence of words as input and
prints the words in a comma separated sequence after sorting them alphabetically.

Example

Suppose the following input is supplied to the program:

order,hello,would,test

Then the output should be:

hello,order,test,would

Solution:

val = raw_input("")
val_list = val.strip().split(",")
val_list.sort()

print ",".join(val_list)

Problem 10:

Write a program which will accept 4 digit binary numbers each separated by a
comma as its input and then check whether they are divisible by 3 or not.
The numbers that are divisible by 3 are to be printed, separated by a comma.

Example

Suppose the following input is given to the program:

0011,0100,0101,1001

Then, the output of the program should be:

0011,1001

Solution:

def toDecimal( binaryNumber ):
    multiplier = 0
    number = 0
    for el in binaryNumber[ : : -1 ]:
        number += int( el ) * ( 2**multiplier )
        multiplier += 1
    return number

bin_value = raw_input("")
bin_list = bin_value.split(",")

result_list = []
for i in bin_list:
    dec_value = toDecimal( i )
    if dec_value % 3 == 0:
        result_list.append(i)

print ",".join(result_list)
Software Solutions