%%file wc.py
import sys
def line_count(filename):
with open(filename) as f:
return len(f.readlines())
def word_count(filename):
with open(filename) as f:
words = f.read().split()
return len(words)
def char_count(filename):
with open(filename) as f:
return len(f.read())
if __name__ == "__main__":
f = sys.argv[1]
print(line_count(f),
word_count(f),
char_count(f),
f)
Overwriting wc.py
!python wc.py poem.txt
21 144 857 poem.txt
!cat poem.txt
The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
!wc poem.txt
21 144 857 poem.txt
problems
Some records are stored with timestamp in database as shown below
records = [("2018-11-11 24:04", "11803", "16602"),
("2018-11-11 24:09", "11803", "16602"),
("2018-11-11 24:14", "11803", "16602"),
("2018-11-11 24:19", "11803", "16602"),
("2018-11-11 24:24", "11803", "16602"),
("2018-11-11 24:30", "11803", "16602"),
("2018-11-11 24:59", "11803", "16602"),
("2018-11-12 01:04", "11803", "16602")]
write a function to correct this from "2018-11-11 24:04" -> "2018-11-12 00:04"
Implement execel function COUNTIFS as a fucntion in python. COUNTIFS(criteria_list, condition). First argument is the list on which condition given as next argument will be performed. and it will returns how many places condition is true.
"<" ----- less than
"<="------ less than or equal to
">"------- greater than
">="------- greater than or equal to
"<>"------- not equal to
"" ---- checks for equal
sample run is
>>> a = [10, 20, 30, 40, 50, 40, 40, 50]
>>> COUNTIFS(a, "<40")
3
>>> COUNTIFS(a, ">=40")
5
>>> COUNTIFS(a, "40")
5
word = "abrakadabra"
def conditional_caps(index, c):
if index%2==0:
return c.upper()
else:
return c.lower()
def alternative_caps(text):
chars = ""
for i, c in enumerate(text):
chars = chars + conditional_caps(i, c)
return chars
alternative_caps(word)
'AbRaKaDaBrA'
def conditional_caps(index, c):
if index%2==0:
return c.upper()
else:
return c.lower()
def alternative_caps(text):
return "".join([conditional_caps(i, c) for i,c in enumerate(text)])
alternative_caps(word)
'AbRaKaDaBrA'
alternative_caps("ksjhdkjshfkjdsfh")
'KsJhDkJsHfKjDsFh'
records = [("2018-11-11 24:04", "11803", "16602"),
("2018-11-11 24:09", "11803", "16602"),
("2018-11-11 24:14", "11803", "16602"),
("2018-11-11 24:19", "11803", "16602"),
("2018-11-11 24:24", "11803", "16602"),
("2018-11-11 24:30", "11803", "16602"),
("2018-11-11 24:59", "11803", "16602"),
("2018-11-12 01:04", "11803", "16602")]
r = records[0]
r
('2018-11-11 24:04', '11803', '16602')
r[0]
'2018-11-11 24:04'
r[0].replace("24:", "00:")
'2018-11-11 00:04'
r[0].split()
['2018-11-11', '24:04']
date, timestamp = r[0].split()
date
'2018-11-11'
timestamp
'24:04'
date.split("-")
['2018', '11', '11']
y, m, d = date.split("-")
def increament(strnum):
return str(int(strnum)+1)
def correct_time(datetime):
date, time = datetime.split()
if "24:" in time:
y, m, d = date.split("-")
d = increament(d)
time = time.replace("24:", "00:")
date = "-".join([y, m, d])
return " ".join([date, time])
else:
return datetime
for r in records:
print(correct_time(r[0]))
2018-11-12 00:04 2018-11-12 00:09 2018-11-12 00:14 2018-11-12 00:19 2018-11-12 00:24 2018-11-12 00:30 2018-11-12 00:59 2018-11-12 01:04
records
[('2018-11-11 24:04', '11803', '16602'),
('2018-11-11 24:09', '11803', '16602'),
('2018-11-11 24:14', '11803', '16602'),
('2018-11-11 24:19', '11803', '16602'),
('2018-11-11 24:24', '11803', '16602'),
('2018-11-11 24:30', '11803', '16602'),
('2018-11-11 24:59', '11803', '16602'),
('2018-11-12 01:04', '11803', '16602')]
for dt, x, y in records:
print(correct_time(dt))
2018-11-12 00:04 2018-11-12 00:09 2018-11-12 00:14 2018-11-12 00:19 2018-11-12 00:24 2018-11-12 00:30 2018-11-12 00:59 2018-11-12 01:04
def correct_records(records):
return [(correct_time(dt), x, y) for dt, x, y in records]
correct_records(records)
[('2018-11-12 00:04', '11803', '16602'),
('2018-11-12 00:09', '11803', '16602'),
('2018-11-12 00:14', '11803', '16602'),
('2018-11-12 00:19', '11803', '16602'),
('2018-11-12 00:24', '11803', '16602'),
('2018-11-12 00:30', '11803', '16602'),
('2018-11-12 00:59', '11803', '16602'),
('2018-11-12 01:04', '11803', '16602')]