Python Modules and Packages, Date and Time Functions, File Handling

 

SUM OF THE INTEGERS IN THE FILE_PY

 Sum of the Integers in the file
Consider an input file "sample.txt" with integer values. Write a program to open the file and read the content to find the sum of all values in the file.

Rule:
The file name should be sample.txt.

Input format:
Give the input as a file which contains the integer values.

Output format:
The output will be the integer which is the sum of the integers. Display the output in the console.

Sample Input file (sample.txt):
1
2
3
4
5

Sample Output:
The sum of the integers in the file sample.txt is:15




NUMBER OF LINES IN THE FILE

Number Of Lines in the file
Write the program to open an input file and read the number of lines in the input file.

Input format:
Input is a file. Filename: input.txt

Output format:
The output will be the integer which is the number of lines in the file. Display the output in the console.

Sample Input file (input.txt):
C was invented to write an operating system called UNIX.
C is a successor of B language which was introduced around 1970
The language was formalized in 1988 by the American National Standard Institue (ANSI).
By 1973 UNIX OS almost totally written in C.
Today C is the most widely used System Programming Language.
Most of the state of the art software have been implemented using C.
Easy to learn
Structured language
It produces efficient programs.
It can handle low-level activities.
It can be compiled on a variety of computers.

Sample Output 1:
The file has 11 lines





UPPERCASE IN FILES

Uppercase in files

Write a program to capitalize the first letter of every word in a file.

Input Format :
Input is a file. Filename: input.txt

Sample Input file (input.txt):
C was invented to write an operating system called UNIX.
C is a successor of B language which was introduced around 1970
The language was formalized in 1988 by the American National Standard Institue (ANSI).
By 1973 UNIX OS almost totally written in C.
Today C is the most widely used System Programming Language.
Most of the state of the art software have been implemented using C.
Easy to learn
Structured language
It produces efficient programs.
It can handle low-level activities.
It can be compiled on a variety of computers.

Sample Output file (output.txt)
C Was Invented To Write An Operating System Called UNIX.
C Is A Successor Of B Language Which Was Introduced Around 1970
The Language Was Formalized In 1988 By The American National Standard Institue (ANSI).
By 1973 UNIX OS Almost Totally Written In C.
Today C Is The Most Widely Used System Programming Language.
Most Of The State Of The Art Software Have Been Implemented Using C.
Easy To Learn
Structured Language
It Produces Efficient Programs.
It Can Handle Low-level Activities.
It Can Be Compiled On A Variety Of Computers.





REVERSE THE FILE

Reverse the file
Write the program to reverse the given text file.

Rule:
The input file name should be input.txt and the output file name should be output.txt.

Sample Input File(input.txt):
A computer is a machine which helps us to calculate, simulate and store different scenarios. For example, in order to write an e-mail, instead of paper and pen first we use a software (or program) called wordprocessor which helps us enter sentences through keyboard (Input), computer's screen (output) to read, and modem (output/input) to send it to a distant relative, friend, etc.

Sample Output File(output.txt):
.cte ,dneirf ,evitaler tnatsid a ot ti dnes ot )tupni/tuptuo( medom dna ,daer ot )tuptuo( neercs s'retupmoc ,)tupnI( draobyek hguorht secnetnes retne su spleh hcihw rossecorpdrow dellac )margorp ro( erawtfos a esu ew tsrif nep dna repap fo daetsni ,liam-e na etirw ot redro ni ,elpmaxe roF .soiranecs tnereffid erots dna etalumis ,etaluclac ot su spleh hcihw enihcam a si retupmoc A

 





SEARCH BLOOD GROUP

Search a Donor
Write a program to display the donor details from the file to the console . Read the blood group detail from the console and search the donors corresponding to the blood group from the file and display the details.

Format of the file.txt file 
Rajkumar,O+ve,Chennai
Chandrabose,B+ve,Chennai
Tamil Selvan,O+ve,Chennai
Ramkumar,B+ve,Chennai
Selvan,O+ve,Chennai

Input Format:

First line of the input consist of blood group.

Output Format:
Display the Donor Details.

[All text in bold corresponds to input and the rest corresponds to output]
Sample Input/Output 1 :
  

Enter blood group to search
B+ve
Donor details
Donor 1
Donor name : Chandrabose
Blood group : B+ve
Donor location : Chennai
Donor 2
Donor name : Ramkumar
Blood group : B+ve
Donor location : Chennai


Sample Input/Output 2 :  
Enter blood group to search
O-ve
Donor details
No donor found for the blood group O-ve





LEAP_YEAR_DATE_TIME

Leap Year
Jack and James they are studing in 2nd class.They are completed all the maths problems .Their teacher was happy with their performance and assigned them to a difficult problem to find out the given date is leap year or not.


Input and output format:
Input consists of String.Date format should be(dd/mm/yyyy),Output contains the given date format is leap year or not.
[All Texts in bold corresponds to the input and rest are output]

Sample Input /Output 1:
Enter the date in format(dd/mm/yyyy)
19/05/1990
The year isn't a leap year!


Sample Input /Output 2:
Enter the date in format(dd/mm/yyyy)
12/05/2000
The year is a leap year!

 



Additional Sample TestCases

Sample Input and Output 1 :
Enter the date in format(dd/mm/yyyy)
12/05/2000
The year is a leap year!
Sample Input and Output 2 :
Enter the date in format(dd/mm/yyyy)
19/05/1990
The year isn't a leap year!




DATE_AGE

Age of the person
 
Ram and Raj are best friends. One day they had a huge discussion about the age calculation. They were confused in the calculation of the age for the given date. So they both are planned to write a program to calculate their ages from the date of birth to given date.
 

Input and output format:
Input consists of String.Date format should be(mm/dd/yyyy ).

1st line of the  input indicates the date of birth and 2nd line of the input indicates the future date.
Output contains
 an integer indicates the current age of the person.
[All Texts in bold corresponds to the input and rest are output]

Sample Input /Output 1:
Enter Birth date as mm/dd/yyyy format:
05/11/1996
Enter Current date as mm/dd/yyyy format:
02/09/2018
The current age of the person is 21

Sample Input /Output 2:
Enter Birth date as mm/dd/yyyy format:
08/05/1953
Enter Current date as mm/dd/yyyy format:
01/04/2018
The current age of the person is 64


 



Additional Sample TestCases

Sample Input and Output 1 :
Enter Birth date as mm/dd/yyyy format:
05/11/1996
Enter Current date as mm/dd/yyyy format:
02/09/2018
The current age of the person is 21
Sample Input and Output 2 :
Enter Birth date as mm/dd/yyyy format:
08/05/1953
Enter Current date as mm/dd/yyyy format:
01/12/2018
The current age of the person is 64





DAYS_OF_THE_WEEK(DATE_TIME)

Name of the day

In a school,teacher asking the question regarding number of weeks most of the students answered correctly ,then teacher asked question like  if you add 'n' number of days to given date what will be the name of the day,most of the student unable to answerd can you help them out to find  the name of the day.


Input and output format:
Input consists of String.Date format should be(mm/dd/yyyy ).
1st line of the  input indicates the date shoulb be(mm/dd/yyyy) format and 2nd line of the input represents integer.

Output contains name of the day after adding 'n' number of days.
Note:Use timedelta().

[All Texts in bold corresponds to the input and rest are output]

Sample Input /Output 1:

Enter the date as mm/dd/yyyy format:
03/09/2018
6
The day is : Thursday

Sample Input /Output 2:
Enter the date as mm/dd/yyyy format:
03/18/2018
2
Thedayis : Tuesday



Additional Sample TestCases

Sample Input and Output 1 :
Enter the date as mm/dd/yyyy format:
03/09/2018
6
The day is : Thursday
Sample Input and Output 2 :
Enter the date as mm/dd/yyyy format:
03/18/2018
2
The day is : Tuesday




DECODING_MONTH_NAME(DATE_TIME)


Decoding Month Names

Ram has created an application  page for registration to Java Course and in that form ,there one field- date of birth. If any person fill the date of birth in(DD-MMM-YYYY) format it should be converted into (DD-MM-YYYY). So help Ram to update registration page that  convert the date of birth  ( DD-MMM-YYYY) format into (DD-MM-YYYY) format.

Input and output format:
Input consists of string.Date format should be(DD-MMM-YYYY).
Output contains string.Date should be in the decoded format.
[All Texts in bold corresponds to the input and rest are output]

Hint:

[Create a dictionary suitable for decoding month names to numbers.
Months in a dictionary should be in the following format: JAN for january , FEB for february, MAR for march and so on.. as keys and the corresponding numbers as values.]


Sample Input /Output 1:
Enter the date in format(DD-MMM-YYYY)
12-DEC-1997
12-12-1997


Sample Input /Output 2:
Enter the date in format(DD-MMM-YYYY)
03-MAR-1975
03-03-1975



 



Additional Sample TestCases

Sample Input and Output 1 :
Enter the date in format(DD-MMM-YYYY)
12-DEC-1997
12-12-1997
Sample Input and Output 2 :
Enter the date in format(DD-MMM-YYYY)
03-MAR-1975
03-03-1975




STOP_WATCH(DATE_TIME)

Stop Watch


Sita and Seema are best friends. One day they had a huge discussion about the time calculation. They were confused to find the diffrence between starting time and end time in milliseconds. So they both are planned to write a program to Difference between two time stamps in milliseconds.

 
Input and output format:
Input consists of String.Time format should be(HH:MM:SS ).Hours format should be '24 hours'.
1st line of the  input indicates the start time and 2nd line of the input indicates the end time.
Output contains an integer indicates the difference between two times in milli seconds.

[All Texts in bold corresponds to the input and rest are output]

Sample Input /Output 1:
Start time
11:00:00
End time
12:10:00
Time in milliseconds is 4200000

Sample Input /Output 2:
Start time
10:00:00
End time
12:10:00
Time in milliseconds is 7800000




 



Additional Sample TestCases

Sample Input and Output 1 :
Start time 
11:00:00
End time 
12:10:00
Time in milliseconds is 4200000
Sample Input and Output 2 :
Start time 
10:00:00
End time 
12:10:00
Time in milliseconds is 7800000




NON-REAPEATING CHARACTER

Module: Non-Repeating Character


Megha and Akila are playing string games. And Megha gives a string to Akila, and she has to find a first non-repeating character from that string. So help Akila by writing a program to find the first non-repeating character from that string. 

Problem Constraints:
Create a Module named StringOpr.py contains a function named nonRepeat( str ):- which returns first non repeating character and sorted string.


Input Format:
The input consists of a string.

Output Format:  
The output consists of a character that represents the non-repeating characters and string in sorted order.

If there is no non-repeating character in the string, then print '#'.

Note: All text in bold corresponds to input and the rest corresponds to output.


Sample Input 1: 
swiiss
Sample Output 1:

w
iisssw

Sample Input 2:
naddan
Sample Output 2:

#
aaddnn






MODULE: STRING LAPINDROME

Module: String Lapindrome

Ram and Ravi are good at string concepts. And Ram wants to check the capability of Ravi, whether she is good at string concepts or not. So he gave one string program to him. He has to find the string is Lapindrome or not. He explains the concept of Lapindrome is like:
Lapindrome is a string which divides the string into two halves, and check the letter frequency of both the strings same or not. If the string contains an odd number of characters, then we have to ignore the middle character.


So help him by writing a program to find the
 given string is Lapindrome or not.

Functional Requirements:
  • Use def myFun () to read the multiple inputs to a function.
  • Use import statements as per program requirements.


Input Format:
The input consists of a string.

Output Format:
The output is a string consists of "Lapindrome" or "Not a Lapindrome".

Note: All text in bold corresponds to input and the rest corresponds to output.

Sample Input 1:
sahas
Sample Output 1:

Lapindrome


Sample Input 2:
abcabd
Sample Output 2:

Not a Lapindrome

 




PACKAGE FOR NEAREST PRIME/PALINDROME

Packages for nearest Prime and Palindrome numbers
Python Packages
Suppose we have developed a very large application that includes many modules. As the number of modules grows, it becomes difficult to keep track of them all, if they are dumped into one location. This is particularly so if they have similar names or functionality.
we might wish for a means of grouping and organizing them
. So, Packages allow for a hierarchical structuring of the module namespace using dot notation. In the same way that modules help avoid collisions between global variable names, packages help avoid collisions between module names.
     
Package Initialization
If a file named __init__.py is present in a package directory, it is invoked when the package or a module in the package is imported. This can be used for the execution of package initialization code, such as initialization of package-level data.
 
Now, let us try to develop some simple programs, which will help us to get an understanding of Python Packages.

Write a program to create a package that contains modules to perform the following operations 
1. nearest prime :
     if the number itself is prime no need to find, return that number itself, or else search for nearest, it can be to the left as well as to the right, if the distance between both numbers is the same then return left prime number. 
2. palindrome number :
     Check whether the given number is palindrome or not

 Problem Constraints:
Create a package named as numPackage, which will contain 2 modules:
Include __init__.py file in a package.
1.Palindrome.py
         Create a method called numPal( number) which will return 1 if the number is palindrome otherwise 0.
2.NearPrime.py
         Create a method called prime( num), which is used to identify whether the number is prime or not.
         Create a method called nearestPrime( num ), which will return the nearest prime number for a given number.
         Note: Nearest prime can be either left or right of the number, absolute distance between them should be minimum. if number itself prime return as it is.

 

Input Format:
The line of the input contains a number denoting the number need to check.

Output Format:
The output will be lined containing Messages, which will indicate whether the entered number is palindrome or not / nearest prime number for a given number
based on the conditions.

Note : If the user 
enters a choice other than 1 or 2 , then exit the program.
Refer to sample input and output for formatting specifications.

Note:  All the bold lines in sample I/O format indicate input and rest correspond to output.

 


Sample I/O format 1:

Enter the choice
1.Nearest Prime number
2. Checking Palindrome number
3.Exit
1
Enter the number
144
Nearest Prime number is 139
Do you want to continue?? (y/n)
y
Enter the choice
1.Nearest Prime number
2. Checking Palindrome number
3.Exit
2
Enter the number
121
121 is Palindrome number
Do you want to continue?? (y/n)
n


Sample I/O format 2:
Enter the choice
1.Nearest Prime number
2. Checking Palindrome number
3.Exit
2
Enter the number
12345
12345 is not Palindrome number
Do you want to continue?? (y/n)
y
Enter the choice
1.Nearest Prime number
2. Checking Palindrome number
3.Exit
1
Enter the number
145
Nearest Prime number is 149
Do you want to continue?? (y/n)
n



Additional Sample TestCases

Sample Input and Output 1 :
Enter the choice
1.Nearest Prime number
2. Checking Palindrome number
3.Exit
1
Enter the number
144
Nearest Prime nuumber is 139
Do you want to continue?? (y/n)
y
Enter the choice
1.Nearest Prime number
2. Checking Palindrome number
3.Exit
2
Enter the number
121
121 is Palindrome number
Do you want to continue?? (y/n)
n
Sample Input and Output 2 :
Enter the choice
1.Nearest Prime number
2. Checking Palindrome number
3.Exit
2
Enter the number
12345
12345 is not Palindrome number
Do you want to continue?? (y/n)
y
Enter the choice
1.Nearest Prime number
2. Checking Palindrome number
3.Exit
1
Enter the number
145
Nearest Prime number is 149
Do you want to continue?? (y/n)
n
Sample Input and Output 3 :
Enter the choice
1.Nearest Prime number
2. Checking Palindrome number
3.Exit
1
Enter the number
354
Nearest Prime number is 353
Do you want to continue?? (y/n)
y
Enter the choice
1.Nearest Prime number
2. Checking Palindrome number
3.Exit
2
Enter the number
787
787 is Palindrome number
Do you want to continue?? (y/n)
y
Enter the choice
1.Nearest Prime number
2. Checking Palindrome number
3.Exit
1
Enter the number
1000
Nearest Prime number is 997
Do you want to continue?? (y/n)
y
Enter the choice
1.Nearest Prime number
2. Checking Palindrome number
3.Exit
3

Problem Requirements:

Python2.7

KeywordMin CountMax Count
def1-
KeywordMin CountMax Count
import1-






USING PACKAGE_MODULE

BASIC MATHEMATICAL OPERATIONS USING PACKAGE

As we know, In Python, package, and modules interesting topics which reduce the number of lines. Packages are namespaces that contain multiple packages and modules themselves. They are simple directories. and a module is a piece of software that has a specific functionality.
 
Now let us try to develop our own package and modules and see it working
So Write a python program which first gives three options, first module addition, second module addition of an odd number and even numbers and last to check whether a given number is jubliee or not.


Requirement functionalities:
  • it must include __init__.py file in the package
  • Create a new package named "package" and the three modules.
  • Modules names as addition,oddsum,jubliee
  •  import all the modules of package into the main program.
Input format:
  • First integer input will be choice of the given options
  • if choice is 1, then it must intake 3 integer values as input(i.e x,n,m > 0)
  • if the choice is 2, then it takes the input of single integer
  • if the choice is 3, then it takes the input of single integer
Output format:
  • for choice 1, output will be result of modulo addition(n,x,m): r=(n+x)%m
  • for choice 2, the output contains two integers first sum of even numbers and sum of odd numbers.
  • for choice 3, the output contains a string stating whether a given number is Jubliee or not. (i.e numbers should start and end with 25) 
Note:  All the bold lines in sample I/O format indicate input and rest correspond to output.

Sample input 1 and sample output 1:
1.addition Modulo
2.Sum of odd and even
3:jubliee or not
1
55
7
8
6

Do you want to continue?(y/n)
y
1.addition Modulo
2.Sum of odd and even
3:jubliee or not
2
2314
6 4

Do you want to continue?(y/n)
y
1.addition Modulo
2.Sum of odd and even
3:jubliee or not
3
2567877625
jubliee
Do you want to continue?(y/n)
n
sample input 2 and Sample output 2:

1.addition Modulo
2.Sum of odd and even
3:jubliee or not
3
23242223

Do you want to continue?(y/n)
y
1.addition Modulo
2.Sum of odd and even
3:jubliee or not
2
4444
16 0

Do you want to continue?(y/n)
n



Additional Sample TestCases

Sample Input and Output 1 :
1.addition Modulo
2.Sum of odd and even
3:jubliee or not
1
55
7
8
6
Do you want to continue?(y/n)
y
1.addition Modulo
2.Sum of odd and even
3:jubliee or not
2
2314
6 4
Do you want to continue?(y/n)
y
1.addition Modulo
2.Sum of odd and even
3:jubliee or not
3
2567877625
jubliee
Do you want to continue?(y/n)
n
Sample Input and Output 2 :
1.addition Modulo
2.Sum of odd and even
3:jubliee or not
2
4343
8 6
Do you want to continue?(y/n)
y
1.addition Modulo
2.Sum of odd and even
3:jubliee or not
3
25687625
jubliee
Do you want to continue?(y/n)
y
1.addition Modulo
2.Sum of odd and even
3:jubliee or not
1
109
5
10
4
Do you want to continue?(y/n)
n
Sample Input and Output 3 :
1.addition Modulo
2.Sum of odd and even
3:jubliee or not
3
23242223
not jubliee
Do you want to continue?(y/n)
y
1.addition Modulo
2.Sum of odd and even
3:jubliee or not
2
4444
16 0
Do you want to continue?(y/n)
n






Comments

Post a Comment

Popular posts from this blog

2-D Arrays

String

Conditional Statements