1 / 6
Aug 2023

I am upskilling so please excuse what may seem like a dumb mistake; please just gently correct. All the solutions I’m finding online are in C++ and this is just my best attempt at thinking through it in Python. I’ve improved from NZEC and compiling error to “wrong answer.” I know I’m close.


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])
  • created

    Aug '23
  • last reply

    Aug '23
  • 5

    replies

  • 489

    views

  • 2

    users

  • 1

    link

I’m no Python expert, but where do you read the input or write the output?

I’ve had to guess the correct indentation for the first part of the code. Please could you edit your post to include the entire code either indented by four spaces, or put three back ticks ``` on a line by themselves both before and after the code. This will stop the smart editor from trying to pretty-print it.

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()

Yes, you always need to read some input and write some output. (There are one or two problems with no input, or fixed input, but they’re the rare exception rather than the rule.)

For CENCRY, you first need to read the number of test cases, then for each test case, read a text, encrypt it, and write the result.

In the example input, this is

2
baax
aaa

Meaning there are two test cases, and these are baax and aaa.

Be aware that this is just an example. The real, hidden test cases will be longer, more complex, and exersize the full range of allowed data. So expect to encrypt a string of 50000 characters for example.

So, in your case, it looks like you need to read the number of test cases, then for each test case, read the string to be encrypted, then pass it to the encryption method before writing the result.

Thanks for the help! I need to get a better feel of the lay of the land of what is being asked for in this kind of coding, as it really is quite different from what has been asked of us in my data science masters (remarkably), but I’m excited to learn, and being willing to be brave and try something new is the first step.