-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython for Data Engineer L2.py
More file actions
139 lines (94 loc) · 3.32 KB
/
Python for Data Engineer L2.py
File metadata and controls
139 lines (94 loc) · 3.32 KB
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
#!/usr/bin/env python
# coding: utf-8
# Program 1: Write a python program using regex or by any other method to check. if the credit card number given is valid or invalid. your python program should read the credit card number from input
#
# In[12]:
import re
PATTERN='^([456][0-9]{3})-?([0-9]{4})-?([0-9]{4})-?([0-9]{4})$'
def is_valid_card_number(sequence):
"""Returns `True' if the sequence is a valid credit card number.
A valid credit card number
- must contain exactly 16 digits,
- must start with a 4, 5 or 6
- must only consist of digits (0-9) or hyphens '-',
- may have digits in groups of 4, separated by one hyphen "-".
- must NOT use any other separator like ' ' , '_',
- must NOT have 4 or more consecutive repeated digits.
"""
match = re.match(PATTERN,sequence)
if match == None:
return False
for group in match.groups():
if group[0] * 4 == group:
return False
return True
def main():
cc_num = str(input('Enter at 16-digit credit card number: '))
if not is_valid_card_number(cc_num):
print('Invalid credit card number')
else:
print('valid card number')
main()
# Program 2: Write a python program to output the following look and say sequence of numbers. It should print
# up to 10 numbers
#
# In[14]:
from itertools import groupby
def lookandsay(number):
return ''.join( str(len(list(g))) + k
for k,g in groupby(number) )
numberstring='1'
for i in range(10):
print(numberstring)
numberstring = lookandsay(numberstring)
# Program 3 : Convert the given roman number in to decimal number. The syntax of roman number is given below . Write the python program using functools
# In[4]:
class Solution(object):
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
roman = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000,'IV':4,'IX':9,'XL':40,'XC':90,'CD':400,'CM':900}
i = 0
num = 0
while i < len(s):
if i+1<len(s) and s[i:i+2] in roman:
num+=roman[s[i:i+2]]
i+=2
else:
num+=roman[s[i]]
i+=1
return num
ob1 = Solution()
print(ob1.romanToInt("XL"))
print(ob1.romanToInt("CDXLIII"))
# Program 4 :Write a python proram to count the occurences of string two in string one. Your python program should read the strings from input
#
# In[1]:
# define string
string = str(input('Enter First String: '))
substring = str(input('Enter Second String: '))
def occurrences(string, substring):
count = 0
start = 0
while True:
start = string.find(substring , start) + 1
if start > 0:
count+=1
else:
return print("The count is:", count)
# print count
occurrences(string, substring)
# Program 5 :write a python program to reverse the words in a given sentence as given below. your python program should read the sentence from input
# In[3]:
def rev_sentence(sentence):
# first split the string into words
words = sentence.split(' ')
# then reverse the split string list and join using space
reverse_sentence = ' '.join(reversed(words))
# finally return the joined string
return reverse_sentence
if __name__ == "__main__":
input = str(input('Enter Sentance: '))
print(rev_sentence(input))