Question
I need help formatting my code, so my output looks like this. TXT FILE 12-30-2020 313 HellO! Bye235 11 Cdc What? Not now! Tues abba
I need help formatting my code, so my output looks like this.
TXT FILE
12-30-2020 313 HellO! Bye235 11 Cdc What? Not now! Tues abba HILLS 02-29-2019 78087 444-22-888 9878 7
MY CODE
import os
from datetime import datetime
#file name input
def readData():
all=[]
file=input('Enter file name with extension: ')
if os.path.isfile(file):
fobj = open (file , "r")
tmpStr = fobj.read()
Lines = file1.readlines()
for line in Lines:
all=line.strip(' ')
all.append(x)
else:
return []
#I found strptime online at programwiz and on the python glossary.
#I used it beacuse it creates a datetime from the string/file
def isValidDate(string):
try:
if string != datetime.strptime(string, "%m-%d-%Y").strftime('%m-%d-%Y'):
raise ValueError
return True
except ValueError:
return False
#Reverse to see if the string is eqaul or not.
def isPalindrome(string):
rev = reversed(string)
if list(string) == list(rev):
return True
return False
#Accepts a string and checks to see how many letters are uppercased
#If it has 1 the count will look for another.
def hasAtLeastTwoUppercase(string):
count=0
for i in string:
if (i.isupper()):
count=count+1
else:
pass
if count >=2:
return True
else:
return False
#int(i)%2!=0 is looking for odd digits and if count is greater then or eqaul
#to 2 it will give give both odd digits
def hasAtLeastTwoOddDigits(string):
count=0
for i in string:
if i.isnumeric():
if int(i)%2!=0:
count=count+1
else:
pass
if count >=2:
return True
else:
return False
#looking fo the last character in a string [-1:]
def endsWithS(string):
if string[-1:]=='s' or string[-1:]=='S' :
return True
else:
return False
def ending(string):
if isValidDate(string):
date.append(string)
if isPalindrome(string):
palin.append(string)
if hasAtLeastTwoUppercase(string):
upper.append(string)
if hasAtLeastTwoOddDigits(string):
odd.append(string)
if endsWithS(string):
ends.append(string)
date=[]
palin=[]
upper=[]
odd=[]
ends=[]
for i in all :
ending(i)
print("Valid Dates:", date)
print("Palindromes", palin)
print("At least TWO Uppercase", upper)
print("At least TWO odd digits", odd)
print("Ends with s or S", ends)
# I took what i did in lab 3 and brought it into here.
#I set it up to look for the vowels in the string
#and if found with 2 return them to a list
def countVowels(string):
vowels=['a','e','i','o','u','A','E','I','O','U']
string=string.lower()
count=0
for i in string:
if i in vowels:
count = count + 1
if count>=2:
return True
else:
return False
vow=[]
for i in all:
if countVowels(i):
vow.append(i)
print("Has TWO or more Vowels",vow)
Valid dates: ( 12-30-2020] Palindromes: ( 313, 11, Cdc, 232, abba, 78087, 7 ] At least TWO Uppercase: ( Hello!, What? Not now! Tues, HILLS ] At least TWO odd digits: [ 12-30-2020, 313, Bye235, 11, 02-29-2019, 78087,9878] Ends with sors: [ What? Not now! Tues, HILLS ]Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started