CS50P Problem set 2 camelCase

Giuseppetringale
3 min readMay 10, 2022

--

Hi everyone, today I would like to present to you the solution I made up regarding the problem set camelCase of CS50P.

In camelCase we need to code a program that asks the user for a name in camel case and output the corresponding name in snake case.

As the example below.

First try

At first I had a lot of trouble solving the problem. So much so that this first solution has a “hard coded” part in line 10. that

if i > 11

was needed to pass the test, but not to write clean and 100% working code. Even leaving out that line, I find the code itself very ugly, in any case now I will try to explain what I did and why.

I started by declaring some variables:

i, n and snake, then on line 4 there is a conditional “if n == n.lower()” (n.lower is a python string method that

Return a copy of the string with all the cased characters converted to lowercase.

More about Python str methods in Python documentation)

So if the input is all in lowercase, my output will be the same. But if the input isn’t all in lowercase characters, the compiler will instead go on line 7, in the else state.

If this happens a while loop will start, until “i” (that at the beginning of the loop is 0) is less than the lenght of the input. As I said this code is of very bad quality, but I will try to briefly explain how the loop works.

The loop starts and checks every single character of the input, whenever the character is lowercase, it increases the count of “i”, every time the char is uppercase the loop checks the count of i.

If i is greater than 11 it updates the value of snake, adding ”_” between all the chars before the i + 1 position and the chars after the i + 1 position.

If on the other hand, i is <= 11 everything is the same, only the position in which is added changes, which is before and after i

To comprehend the slicing method that I used, I suggest you to check this brilliant source.

At the end I just told the compiler to print the new version of snake, but all in lower case with the lower() method.

My optimal solution

After understanding better the uses of loops I ended up on this final solution, much more clean, Pythonian and… correct

This is more readable than the other, and the explanation will be a piece of cake.

  • We start initializing a variable, called camelcase, that it’s our input.
  • Then we print “snake_case: “ changing the end argument of the print to nothing (instead of the new line (\n)
  • On line 3 starts a loop that iterate every char in our input
  • If the char is in lowercase, we print just that char (changing the end of the print as before)
  • else if the char is uppercase we print “_” and the corresponding char in lowercase
  • And out of the loop we use the print function (without changing end) just to print another line for the aesthetic of the result.

--

--

Giuseppetringale

Hi dear reader! I’m Giuseppe Tringale I was born in Catania, Italy, in 2001. I’m currently doing a bachelor in Business economics and I’m self studying CS!