DISCHAR (using Python)

I am a novice at Python, and fully new to CodeChef. I feel like the November 2014 “DISCHAR” problem ( DISCHAR Problem - CodeChef ) should be (/is?) within my ability level, but my solutions all flunked. Any help understanding where I went wrong (code itself, understanding the question, understanding what format the input will be, some aspect of submitting to this particular site) would be very much appreciated.

Here is my code, which seems to work fine in the interpreter I’m using:

def dsc(t):
	used = ""
	for lets in t:
	    if lets not in used:
	        used = used + lets
	print len(used)

What am I doing wrong?

A subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.

So now moving towards the question, we can simply do this question by sets.
A set is a well defined collection of distinct objects.

pseudocode:

1) take string as input

2) insert each character in a set

3) output the size of set

Now in python you use the set function predefined in the python library and display its size.

Hope this will help. :slight_smile:

Here is your updated code:

    def dsc(s):
	used = ""
	for lets in s:
		if lets not in used:
			used = used + lets
			
	print len(used)
	
    t = int(raw_input())
    while t:
	    s = raw_input()
   	    dsc(s)
	t -= 1

@ems314

how can you say your code works fine on interpreter?

I referred your last attempt and saw this code, and it seems your logic in dsc() is correct but…

t = input ()
def dsc(t):
	used = ""
	for lets in t:
	    if lets not in used:
	        used = used + lets
	print len(used)   

First observe problem what the problem says:

Input:
First line contains T, number of testcases. Each testcase consists of a single string in one line. Each character of the string will be a small alphabet(ie. ‘a’ to ‘z’).

that means, first input contains an integer t, representing number of test cases,

so you have to take that value into consideration ,

t=int(raw_input())

int() is used to typecast string input into integer.

Then you have to take string for operation as input for t times as t represents number of test cases so,

for num in range(t):
    string=input()

These two steps are mandatory now you may implement any logic…

Considering your logic…

First let me tell you what mistake you committed,

Step 1: You took an input t

Step 2: you defined a function dsc() and implemented your logic inside your function… and end…

But this is not the end, if you have defined a function, you have to call the function by passing the required arguments,

In your case, first you have to take no. of test cases as input, let test is that variable so just add this line above all lines,

test=int(raw_input())

Then you took t as string input but you have to take that input for test times so it should be,

for num in range(test):
    t = raw_input()

then defining function and implementing code in function is good, but all this you were coding above function definition were the part of main function so cut paste dsc() at the top of your program, thus your code becomes like this,

def dsc(t):
    used = ""
    for lets in t:
        if lets not in used:
            used = used + lets
    print len(used)

test=int(raw_input())
for num in range(test):
    t = raw_input()

now everything is set, just you have to call the function, which is done as,

dsc(t)

so add this piece in main function after taking t as string input, inside for loop…

So your final code becomes…

 #dsc() 
def dsc(t):
    used = ""
    for lets in t:
        if lets not in used:
            used = used + lets
    print len(used)
    

 # main()
test=int(raw_input())
for num in range(test):
    t = raw_input()
    dsc(t)

Check this Submission link :CodeChef: Practical coding for everyone

Hope you have understood the basic concept… :slight_smile:

Try this your self… :slight_smile:

you have implemented the correct logic.
all you need is to pass the string which is taken input to the function dsc. :slight_smile: