Hi! I’m super new to competitive programming/ SPOJ stuff, so I guess I missed the part about reading input/writing output, and maybe that’s why my code is failing. Are you supposed to read input and write output every time, with SPOJ stuff/ competitive programming problems?
I’m 95% of the way finished a data science masters, but none of the classes so far have required the kind of generalized input/output function writing that is asked for here. It was all pd.read_csv for the most part for input, or for any os.read stuff the professor provided significant hints or an entire template for the code. So, I’m learning. My goal with practicing here is to fill in the gaps that somehow weren’t taught in the masters program, so I can pass the coding skills test for a remote programming job.
Here’s my code from last night, formatted as you requested. I’m giving up on it but leaving it up for future ppl’s learning processes.
# your code goes here
import itertools
import re
vowels="aeiou"
consonants="bcdfghjklmnpqrstvwxyzbcdfghjklmnpqrstvwxyz"
longvowels=""
longconsonants=""
sum_value = 0
for i in itertools.cycle(vowels):
longvowels=longvowels+i
sum_value += 1
if sum_value > 50000:
break
sum_value = 0
for i in itertools.cycle(consonants):
longconsonants=longconsonants+i
sum_value += 1
if sum_value > 50000:
break
ccount = {}
result = ''
def encryption(t):
for c in t:
if c in ccount:
ccount[c] += 1
else:
ccount[c] = 1
k = (ccount[c]-1)
if c in vowels:
#I want to find the kth occurence of c in infinitevowels
#then find the index of this kth c
#then print the same index number character within infiniteconsonants
def encrypt(c):
for i in range(len(longvowels)):
newcount=0
if i == c:
newcount+=1
if newcount == k:
indices = [c for c in re.finditer(c, longvowels)]
desiredindex=indices[k-1]
#this above is supposed to be returning the index of the
#desired character
result.append(longconsonants[desiredindex])
else:
#I want to find the kth occurence of c in infiniteconsonants
#then find the index of this kth c
#then print the same index number character within infinitevowels
def encrypt2(c):
for i in range (len(longconsonants)):
newcount=0
if i == c:
newcount+=1
if newcount == k:
indices = [c for c in re.finditer(c, longconsonants)]
desiredindex=indices[k-1]
result.append(longvowels[desiredindex])
I also used my librarian skills (I’m a librarian by training, transitioning into data science) to convert an existing successful C++ code into Python. I found an AI robot that converted this passable C++ code into Python. I’m attaching both below.
C++ (credit neeraj08)
#include<stdio.h>
int main()
{
int t,i,pos,cnt[26];char c,s[50001],cs[]={'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z'},vs[]={'a','e','i','o','u'};
scanf("%d\n",&t);
while(t--)
{
for(i=0;i<26;cnt[i++]=0);
while((c=getchar())!='\n'&&c!=EOF)
{
cnt[c-97]++;
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u')
{
for(pos=0;vs[pos]!=c;pos++);
putchar(cs[((cnt[c-97]-1)*5+pos)%21]);
}
else
{
for(pos=0;cs[pos]!=c;pos++);
putchar(vs[((cnt[c-97]-1)*21+pos)%5]);
}
}
putchar('\n');
}
return 0;
}
Python AI conversion:
import sys
def main():
t = int(input())
for _ in range(t):
cnt = [0] * 26
s = input().strip()
for c in s:
cnt[ord(c) - ord('a')] += 1
if c in ['a', 'e', 'i', 'o', 'u']:
pos = ['a', 'e', 'i', 'o', 'u'].index(c)
sys.stdout.write(['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'][((cnt[ord(c) - ord('a')] - 1) * 5 + pos) % 21])
else:
pos = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'].index(c)
sys.stdout.write(['a', 'e', 'i', 'o', 'u'][((cnt[ord(c) - ord('a')] - 1) * 21 + pos) % 5])
sys.stdout.write('\n')
if __name__ == "__main__":
main()