python program which return max count of chacharter like input=aabbbcczaaaabbbccc ouput=a3b3c3z1
st=aabbbcczaaaabbbccc
ouput=a3b3c3z1
def maxCount(st):
return
maxCount(st)
If you meant max consecutive times, then this code will work (it returns a dictionary):
def maxCount(st):
counts = dict()
last = ''
tmp = 0
for c in st:
if last == c:
tmp += 1
elif last != '':
if last in list(counts.keys()):
counts[last] = max(counts[last], tmp)
tmp = 1
else:
counts[last] = tmp
tmp = 1
last = c
return counts
print(maxCount('aabbbcczaaaabbbccc'))
Need to find the count of each consecutive character in a row.
Ex: aaaabbBBccaa
output: a4b2B2c2a2
The character may repeat but need to count only consecutive ones. I also need to maintain the original sequence.
I tried to do this by code below but it doesn't work.
l = input()
counter = dict()
if l.isalpha() == True:
for letter in l:
if letter in counter:
counter[letter] += 1
else:
counter[letter] = 1
for this_one in list(counter.keys()):
print(this_one,counter[this_one],sep ="",end="")
Solution if you are interested in the while loop mechanics :
l = 'aaaabbBBccaazzZZZzzzertTTyyzaaaAA'
output = ''
index = 0
while index < len(l):
incr = index
count = 1
output += l[incr]
while incr < len(l)-1 and l[incr]==l[incr+1]:
count += 1
incr += 1
index += 1
output += str(count)
index += 1
print(output)
itertools.groupby allows you to express this quite concisely using a generator expression.
from itertools import groupby
''.join(f'{x}{len(list(y))}' for x, y in groupby('aaaabbBBccaa'))
# outputs:
'a4b2B2c2a2'
outlook = Dispatch("Outlook.Application")
mapi = outlook.GetNamespace("MAPI")
your_folder = mapi.Folders['email#outlook.com'].Folders['Blah']
blah_inbox = your_folder.Items
f = open("email_txt.csv", "w")
for message in blah_inbox:
if message.Class == 43:
if message.SenderEmailType == 'EX':
print(message.Sender.GetExchangeUser().PrimarySmtpAddress)
f.write(message.Sender.GetExchangeUser().PrimarySmtpAddress)
else:
print(message.SenderEmailAddress)
I opened a new fie and wrote it to a csv, but I get the following result.
user1#outlook.comuser2#outlook.comuser3#outook.com.....
However, I need the result to be like the below
user1#outlook.com
user2#outlook.com
user3#outlook.com
The Simple solution is the following
f = open("email_txt.csv", "w")
for message in blah_inbox:
if message.Class == 43:
if message.SenderEmailType == 'EX':
print(message.Sender.GetExchangeUser().PrimarySmtpAddress)
f.write(message.Sender.GetExchangeUser().PrimarySmtpAddress + '\n')
else:
print(message.SenderEmailAddress)
I added a "\n" to the end of the write command
Alternatively, since my whole intention was to save in a CSV with some sort of headers. Below is what worked for me. This is the entire code.
Todays_Mail = dt.datetime.now() - dt.timedelta(hours=24)
Todays_Mail = Todays_Mail.strftime('%m/%d/%Y %H:%M %p')
# Connect to Outlook inbox
outlook = Dispatch("Outlook.Application")
mapi = outlook.GetNamespace("MAPI")
your_folder = mapi.Folders['email'].Folders['Blah']
blah_inbox = your_folder.Items
blah_inbox = blah_inbox.Restrict("[ReceivedTime] >= '" + Todays_Mail + "'")
f = open("email.csv", "w")
f.write('Emails,Index\n')
index = 0
for message in blah_inbox:
if message.Class == 43:
if message.SenderEmailType == 'EX':
print(message.Sender.GetExchangeUser().PrimarySmtpAddress)
f.write(message.Sender.GetExchangeUser().PrimarySmtpAddress + ',' + str(index) + '\n')
index = index + 1
else:
print(message.SenderEmailAddress)
f.close()
The above will give you two columns, one with the header Email, and the second would be a column with the header Index.
I am having a problem with my code trying to do an advanced caesar cipher shift. I changed a certain letter to another, and then added a string to certain parts of a file before encoding, but am having problems doing the shifting now. This is my code:
import string
import sys
count = 1
cont_cipher = "Y"
#User inputs text
while cont_cipher == "Y":
if count == 1:
file = str(input("Enter input file:" ""))
k = str(input("Enter shift amount: "))
purpose = str(input("Encode (E) or Decode (D) ?: "))
#Steps to encode the message, replace words/letter then shift
if purpose == "E":
my_file = open(file, "r")
file_contents = my_file.read()
#change all "e"s to "zw"s
for letter in file_contents:
if letter == "e":
file_contents = file_contents.replace(letter, "zw")
#add "hokie" to beginning, middle, and end of each line
lines = file_contents.split('\n')
def middle_message(lines, position, word_to_insert):
lines = lines[:position] + word_to_insert + lines[position:]
return lines
new_lines = ["hokie" + middle_message(lines[x], len(lines[x])//2, "hokie") + "hokie" for x in range(len(lines))]
#math to do the actual encryption
def caesar(word, shifts):
word_list = list(new_lines)
result = []
s = 0
for c in word_list:
next_ord = ord(c) + s + 2
if next_ord > 122:
next_ord = 97
result.append(chr(next_ord))
s = (s + 1) % shifts
return "".join(result)
if __name__ == "__main__":
print(caesar(my_file, 5))
#close file and add to count
my_file.close()
count = count + 1
The error I am getting is:
TypeError: ord() expected a character, but string of length 58 found
I know that I need to split it into individual characters, but am not sure how to do this. I need to use the updated message and not the original file in this step...
I just started with python3 and tried this idea of assigning number to alphabets and calculate the total.
Eg: if input is "Hi" my output should come "6" (H is 5 and I is 1 so total is 6)
I do not know how to sum the output of while loop output.
name = input("Enter Your name ")
name =name.upper()
name = list(name)
print(name)
items = {'A':'1', 'I':'1', 'J':'1', 'Q':'1','Y':'1',
'B':'2', 'K':'2', 'R':'2',
'C':'3', 'G':'3', 'L':'3', 'S':'3',
'D':'4', 'M':'4', 'T':'4',
'E':'5', 'H':'5', 'N':'5', 'X':'5',
'U':'6', 'V':'6', 'W':'6', 'O':'7', 'Z':'7', 'F':'8', 'P':'8', '.':'0'}
counter = 0
x = len(name)-1
while counter <=x:
names = name[counter]
if names in items:
new_name = (items[names])
else:
print('no')
name_int = int(new_name)
print(name_int)
counter = counter +1
This should work for you:
name = input("Enter Your name ")
name =name.upper()
name = list(name)
print(name)
items = {'A':'1', 'I':'1', 'J':'1', 'Q':'1','Y':'1',
'B':'2', 'K':'2', 'R':'2',
'C':'3', 'G':'3', 'L':'3', 'S':'3',
'D':'4', 'M':'4', 'T':'4',
'E':'5', 'H':'5', 'N':'5', 'X':'5',
'U':'6', 'V':'6', 'W':'6', 'O':'7', 'Z':'7', 'F':'8', 'P':'8', '.':'0'}
counter = 0
x = len(name)-1
total = 0
while counter <=x:
names = name[counter]
if names in items:
new_name = (items[names])
total += int(new_name)
else:
print('no')
counter += 1
print(total)
However, you can write your code in a more pythonic way:
name = input("Enter Your name ")
items = {'A':1, 'I':1, 'J':1, 'Q':1,'Y':1,
'B':2, 'K':2, 'R':2,
'C':3, 'G':3, 'L':3, 'S':3,
'D':4, 'M':4, 'T':4,
'E':5, 'H':5, 'N':5, 'X':5,
'U':6, 'V':6, 'W':6, 'O':7, 'Z':7, 'F':8, 'P':8, '.':0}
total = 0
for ch in name.upper():
total += items(ch) if ch in items else 0
print total