Skip to content

Lowercase function with test cases for thofstrand#186

Open
thofstrand wants to merge 2 commits intokscanne:masterfrom
thofstrand:function
Open

Lowercase function with test cases for thofstrand#186
thofstrand wants to merge 2 commits intokscanne:masterfrom
thofstrand:function

Conversation

@thofstrand
Copy link

Created function that lower cases a variety of languages based on input. Includes many test cases. Just run the .py file in order to run the tests.

else:
return word

def combineToString(char_list):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see great use of inbuilt functions, but this combineToDefined defined functions could be used in a single line to reduce extra code.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be wise but I also feel as though the main function is already way too large. I think splitting it actually makes for fewer lines of code in the long run.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, i agree to this then.

lowCase = word.lower()
caps = ['A','E','I','O','U','Á','É','Í','Ó','Ú']
if word[0] == 'n' or word[0] == 't':
if word[1] in caps and word[2].isalpha(): #must check isalpha as Õ parses as `O`,`~`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I quiet didn't understand the 3 letter must be a alpha or not. I think just the 1 letter must be a n or a t and the second letter in caps.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Essentially one of the chars ( Õ) that was an input splits into two characters (O,~) when I am separating the string by characters. I used isalpha to check for this and make sure i am not counting that character as one of the listed ones. I do see another issue as I am not checking the length of the word, so this I can fix!


def lowerCase(word, lang):
word = word.strip()
langStart = lang[:2]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add a condition if the user is giving false/errorful language as input.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good thought! I am adding this now!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code change is fine.

@mepripri
Copy link

The code was quite understandable, but some lines could be changed/improved. I loved the use of functions in the code. Just some more test cases are missed. Overall, it was a good piece of code, I got to learn something.

@mepripri
Copy link

I am fine with the changes now. Signing this off :)

Copy link
Owner

@kscanne kscanne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some additional comments I would have made on an initial review.

lang = lang.split('-')
langStart = lang[0]

if len(langStart) != 2:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should look at the BCP-47 spec. There are (many) language codes that have length 3.

langStart = lang[0]

if len(langStart) != 2:
return "Invalid Language"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a place to raise an exception, not return a string error message.

if len(langStart) != 2:
return "Invalid Language"

if "en" == langStart:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why just English? Shouldn't this be the default for many languages that have case?


if "I" in word:
word_list = [*word]
indices = [i for i, x in enumerate(word_list) if x == "I"]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simpler to use the string "replace" method vs. converting to list and back?

caps = ['A','E','I','O','U','Á','É','Í','Ó','Ú']
if word[0] == 'n' or word[0] == 't':
if len(word) > 2:
if word[1] in caps and word[2].isalpha(): #must check isalpha as Õ parses as `O`,`~`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a cleaner way to handle this; will discuss in class.

lower_word_list[-1] = 'ς'
lower_word = combineToString(lower_word_list)
return lower_word
else:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should have many more test cases for other languages like Spanish, French, etc. that are handled like English. These tests will fail given the current code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants