-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab7.py
177 lines (147 loc) · 3.57 KB
/
lab7.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# -*- coding: utf-8 -*-
"""lab7.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1MKAtWIOpgCksI9iFCrpH6i3Aayi_CSgt
1. Write program that take string mix of chars and numbers and
you should extract all numbers and output sum of this
numbers
Input:a10sdsd5
Output:15
"""
def findSum(str1):
temp = "0"
Sum = 0
for ch in str1:
if (ch.isdigit()):
temp += ch
else:
Sum += int(temp)
temp = "0"
return Sum + int(temp)
str1 = input("")
print(findSum(str1))
"""2. Write a program to check whether a given string ends with
the contents of another string
Input s: aswan
Input s2:an
Output:yes
"""
def check(string, sub_str):
if (string.find(sub_str) == -1):
print("NO")
else:
print("YES")
string =input(" ")
sub_str =input("")
check(string, sub_str)
"""3. Write program to check every first char of input string is
capital
Input: iti aswan
Output: Iti Aswan
"""
string =input(" ")
capitalized = string.title()
print(capitalized)
"""4. Write a program to accept a string from the user and display
characters that are present at an even index number.
Input: itiaswan
Print: i a
"""
from collections import Counter
test_str =input(" ")
res = Counter(test_str)
print ( str(res))
for i in res:
if res[i] %2==0:
print(i)
"""5. Create a new list from a two list using the following condition
Given a two list of numbers, write a program to create a new
list such that the new list should contain odd numbers from
the first list and even numbers from the second list.
L1=[1,2,3,4,5,6,7]
L2=[8,9,10,11,12]
Output:[1,3,5,7,8,10,12]
"""
l1=[1,2,3,4,5,6,7]
l2=[8,9,10,11,12]
odd_numbers = []
for i in l1:
if i % 2 == 1:
odd_numbers.append(i)
even_numbers = []
for j in l2:
if j % 2 == 0:
even_numbers.append(j)
new= odd_numbers+ even_numbers
print(new)
"""6. Write a program that take input contain lower and uppercase
chars and append lower case first
Input: ABcDfe
Output: cfeABC
"""
string =input(" ")
newstring =''
new =''
for a in string:
if a.islower() == True:
newstring+=a
print(newstring)
for b in string:
if b.isupper() == True:
new+=b
print(new)
print(newstring+new)
"""7. Write a Python program to generate a random integer
between 0 and 6 , random integer between 5 and 10,
random integer between 0 and 10, with a step of 3. Use
random.randrange()
"""
import random
print(random.randint(0,6))
print(random.randint(5,10))
print(random.randrange(0, 10, 3))
"""8. Write a Python program to shuffle the elements of a given
list. Use random.shuffle()
"""
import random
list1=[1,5,7]
random.shuffle(list1)
print(list1)
"""9. You are given a string (S) and width w.
Your task is to wrap the string into a paragraph of width w.
Input s: abcdefg
Input w: 3
Output:
abc
def
g
anthor input s:itiaswan
Output:2
it
ia
sw
an
"""
t=int(input(" "))
string=input(" ")
import textwrap
print (textwrap.wrap(string,t))
"""10. On February 29, 1996 Rafeef was born, people in our city call
her "leaper".
A person who was born on the 29th of February is called "leaper"
or "leap-year baby". This is a little rare event because in our city
we use the Gregorian calendar, in this calendar there are 28 or 29
days in Febraury (depending on whether the year is a leap or
not).
Refeef doesn't know if she will celebrate her birthday this year or
not, so she asked you to help her by answering her question.
Print YES if the current year is leap. Otherwise print NO
Input :2018
Output:No
"""
year=int(input(""))
if year%4 and year%100 or year%400:
print("yes")
else:
print("no")