Wednesday, October 28, 2015

Write a program, which will find all such numbers between 1000 and 3000

#Question:
#Write a program, which will find all such numbers between 1000 and 3000
#(both included) such that each digit of the number is an even number.
#The numbers obtained should be printed in a comma-separated sequence on a single line.

values=[]
for x in range(1000,3000):
    s=str(x)
    if (int(s[0]) %2 == 0) and (int(s[1]) %2 == 0) and (int(s[2]) %2 == 0) and (int(s[3]) %2 == 0):
        values.append(s)
print "-".join(values)

4 digit binary numbers as its input and then check whether they are divisible by 5 or not.

#Question:
#rite a program which accepts a sequence of comma separated
#4 digit binary numbers as its input and then check whether they are divisible by 5 or not.
#The numbers that are divisible by 5 are to be printed in a comma separated sequence.
#Example:
#0100,0011,1010,1001
#Then the output should be:
#1010
#Notes: Assume the data is input by console.
#
#
#Hints:
#In case of input data being supplied to the question, it should be assumed to be a console input.

#Solution:
value = []
items=[x for x in raw_input().split(',')]
for p in items:
    print p
    intp = int(p, 2)
    print intp
    if not intp%5:
        value.append(p)

print ','.join(val

Lowercase to Uppercase

#Question
#Write a program that accepts sequence of lines as input and prints the lines after making all characters in the sentence capitalized.
#Suppose the following input is supplied to the program:
#Hello world
#Practice makes perfect
#Then, the output should be:
#HELLO WORLD
#PRACTICE MAKES PERFECT

##Hints:
#In case of input data being supplied to the question, it should be assumed to be a console input.

stopword=""
while True:
    str=raw_input()
    if str.strip()==stopword:
        break
print str.upper()
   

Dictionaries

#Question:
#With a given integral number n, write a program to generate a dictionary that contains (i, i*i) such that is an integral number between 1 and n (both included). and then the program should print the dictionary.
#Suppose the following input is supplied to the program:
#8
#Then, the output should be:
#{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}

def createdict(n):
    dictofnumber=dict()
    for i in range(1,n):
        dictofnumber[i]=i * i
    return dictofnumber
   
a=createdict(100)
print a
   

program which will find all such numbers which are divisible by 7 but are not a multiple of 5

#Question:
#Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5,


def mat():
    mylist=[]
    for i in range(2000,3000):
        count=0   
        if (i % 7 == 0) and (i % 5 != 0):
            count=count + 1
            mylist.append(i)
    return mylist

a=mat()
print a
   

Sunday, October 18, 2015

send mail from python

import smtplib

def send_email(sender, receiver, subject, body):
    msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n%s"
        %(sender, receiver, subject, body))
    s = smtplib.SMTP('smtp.gmail.com')
    s.starttls()
    s.login('user@gmail.com','pass')
    s.sendmail(sender, [receiver], msg)
    s.quit()
   
send_email('user@gmail.com','user@gmail','Test mail from script','Regards,Parag')

Draw circle using python

import turtle

 
           
def drawPolygon(t, sideLength, numSides):
    turnAngle = 360 / numSides
    for i in range(numSides):
        t.forward(sideLength)
        t.right(turnAngle)

def drawCircle(anyTurtle, radius):
    circumference = 10 * 3.1415 * radius
    sideLength = circumference / 360
    drawPolygon(anyTurtle, sideLength, 360)


wn = turtle.Screen()
wheel = turtle.Turtle()
turtle.pen(fillcolor="black", pencolor="red", pensize=10)
drawCircle(wheel, 20)
turtle.fillcolor('red')

wn.exitonclick()