My first challenge from CodeEval


So what is CodeEval?

The platform for developer challenges.

CodeEval is a platform used by developers to showcase their skills. Developers can participate in app building competitions and win cash/prizes. They can also solve programming challenges as a way to impress employers with their technical skills. Employers can use CodeEval as a way to enhance their brand by launching competitions/programming challenges and as a means to get introduced to the best developers.

Our Friends

We are mentored by the awesome folks at i/o ventures and are now backed by HireVue Labs, the startup studio arm of HireVue, the world’s leading digital interviewing platform.

Yup, it is a really cool website as you can practice your coding skills at the same time it’s a portfolio of your skillz. My first challenge is called “FizzBuzz” and it’s in the easy level section. Yea, I am a beginner; I am optimistic that I will learn a ton from CodeEval.

The challenge says :

Input sample:

Your program should read an input file (provided on the command line) which contains multiple newline separated lines. Each line will contain 3 numbers which are space delimited. The first number is first number to divide by (‘A’ in this example), the second number is the second number to divide by (‘B’ in this example) and the third number is where you should count till (‘N’ in this example). You may assume that the input file is formatted correctly and is the numbers are valid positive integers. E.g.

3 5 10
2 7 15

Output sample:

Print out the series 1 through N replacing numbers divisible by ‘A’ by F, numbers divisible by ‘B’ by B and numbers divisible by both as ‘FB’. Since the input file contains multiple sets of values, your output will print out one line per set. Ensure that there are no trailing empty spaces on each line you print. E.g.

1 2 F 4 B F 7 8 F B
1 F 3 F 5 F B F 9 F 11 F 13 FB 15

Here is my attempt…

import sys
def fizzbuzz(a,b,l):
	for y in xrange(1,l+1):
		result.append(str((y%a == 0) * 'F' + (y%b == 0) * 'B' or y))
	return result

numbers = open(sys.argv[1], 'r').readlines()

if numbers:
	for x in xrange(0,len(numbers)):
		a,b,l = numbers[x].split(" ")
		print ' '.join(fizzbuzz(int(a),int(b),int(l)))

I have learned a couple of things from this challenge. Using true and false in order to produce a value. Here I wrote (y%a == 0) which will either give me true or false which means it will return either 1 or 0. And you can actually multiply a character with number in order to produce that amount of number of characters.

For example

>>> print 5 * 'c'

Split() usage

>>> x = "Hello how are ya?"
>>> x.split(" ")
['Hello', 'how', 'are', 'ya?']
>>> x.split(" ",2)
['Hello', 'how', 'are ya?']

Split() separates the string into a list, and you can actually split till a certain value as shown in the 4th line.

Join() usage

>>> x
['Hello', 'how', 'are', 'ya?']
>>> ' '.join(x)
'Hello how are ya?'
>>> '|'.join(x)


Join() combines the list values into one string putting whatever interval you specified. In the above example I used space ‘ ‘ and polon ‘|’.

One more thing that I learn is that I can assign the split values into a sequence of variable and it will automatically assign to each variable based on the order. Here is the example:

>>> x
 'A B C'
 >>> a,b,c = x.split(' ')
 >>> print "%r %r %r" %(a,b,c)
 'A' 'B' 'C'

Alright! This is what I have for today and Give CodeEval a try!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>