If you are given three sticks, you may or may not be able to arrange them in a triangle. For any three lengths “if any of the three lengths is greater than the sum of the other two, then you cannot form a triangle. Otherwise, you can.” Write a program that takes three integers as inputs and determine whether you can or cannot form a triangle from sticks with the given lengths.
Answer
#program
to test if 3 sticks can perfectly form a triangle
#inputs:
a =
float(raw_input(“Enter the length of side A?”))
b =
float(raw_input(“Enter the length of side B?”))
c =
float(raw_input(“Enter the length of side C?”))
if
(a > (b + c)):
status = False;
elif
(b > (a + c)):
status = False;
elif
(c > (a + b));
status = False;
else:
status = True;
if
(status):
print “A perfect triangle can be formed”. Format
(a,b,c)
else:
print “A perfect triangle cannot be formed”
Format(a,b,c)
The usual definition of a palindrome is a word that reads the same both forward and backward like “otto”, “ada” and “madam”. Therefore we might say, a single letter is a palindrome and a two-letter word is a palindrome if the letters are the same and any other word is a palindrome if the first letter is the same as the last and the middle is a palindrome. Write a program to read a string and indicate whether the word is palindrome or not.
ANSWER
#program
to test if an input string is a PALINDROME
#method
to reverse the string
def
reverse(string):
new_string = “”
for char in string:
new_string = char + new_string
return new_string
#method
to check if the string is a palindrome
def
isPalindrome(string):
flag = True;
if (len(string) > 0);
reverse_string = reverse(string)
for index in range(0,len(string));
if(string[index] != reverse_string[index]):
flag = False
break
else:
flag = True
else:
print “String must not be empty!”
return flag
#program
section to read an input string
#and
then test if it is a Palindrome
text
= raw_input(“Please type in a string…..?”)
if
(isPalindrome(text)):
print text, “is a PALINDROME!!!!!”
else:
print text, “is not a PALINDROME!!!!!”
A word is said to be “abecedarian” if the letters in the word appear in the alphabetical
order. For example, the following letters are all 6-letters English abecedarian words: abdest,
acorsy, adempt, agnosy, best, behint, beknow, bijoux, biopsy, chintz, deux,
dehort, write a program for checking whether a given word is abecedarian, assuming that the word
contains only lower-case letters.
ANSWER
#program
to test if an input string is ABECEDARIAN
#method
to get the list of ascii codes of all characters in the string
def
get ascii_list(string):
asciis = []
for char in string:
asciis.append(ord(char))
return asciis
#method
to check if the string is a ABECEDARIAN
def
isAbecedarian(string):
flag = True;
if(len(string) > 0):
ascii_codes_list =
get_ascii_list(string)
min_code = ascii_codes_list[0]
for codein ascii_codes_list:
if(min_code > code):
flag = True
min_code = code
else:
print”string must not be
empty!”
return flag
#program
section to read an input string
#and
then test if it is ABECEDARIAN
text = raw_input(“Please type in a string….?”)
if(isAbecedarian(text)):
print text, “is a ABECEDARIAN!!!!”
else:
print text, “is not a ABECEDARIAN!!!!”
State 4 major features of the Python Programming Language.
1…………………………………………………………………………………………………………………….
2…………………………………………………………………………………………………………………….
3…………………………………………………………………………………………………………………...
4……………………………………………………………………………………………………………………
ANSWER
Python is open source
Python is interactive, user-friendly and
easy to learn
Python is platform independent
Python is object-oriented
Python is extensible
Python is a dynamic language
Python supports multiple programming
languages paradigms
Python has a large library of codes
Python has a large user support community
Python is an interpreted high-level
language.
Describe the functions of the following built-in functions with an example each
(a)
raw_input():
Description: is a python function that can be used to read in user inputs
from the keyboard
Example: name = raw_input(“what is your name?”) #reads a name
(b)
round()
Description: is a function used for rounding up a floating-point number
to a given number of precision
Example: val = round(34.56778, 2) #rounds to 2 dec places
(c)
chr()
Description: is a function used for an ASCII code number to its
corresponding ASCII character.
Example: char = chr(65) #converts 65 to ascii character which is ‘A’
(d)
range(n,k)
Description: a function used to
generate a list of numbers between the range of N up to K-1
Example: numbers = range(1, 11) #generates the list
{1,2,3,4,5,6,7,8,9,10}
No comments:
Post a Comment