Skip to content

Instantly share code, notes, and snippets.

@ZechCodes
Created November 9, 2020 00:01
Show Gist options
  • Save ZechCodes/3da9f80f627663037499adca49f6071f to your computer and use it in GitHub Desktop.
Save ZechCodes/3da9f80f627663037499adca49f6071f to your computer and use it in GitHub Desktop.
Challenge 146 - Compare by ASCII Codes

Challenge 146 - Compare by ASCII Codes

Create a function that compares two words based on the sum of their ASCII codes and returns the word with the smaller ASCII sum.

Examples

ascii_compare("hey", "man") ➞ "man"
# ["h", "e", "y"] ➞ sum([104, 101, 121]) ➞ 326
# ["m", "a", "n"] ➞ sum([109, 97, 110]) ➞ 316

ascii_compare("majorly", "then") ➞ "then"

ascii_compare("victory", "careless") ➞ "victory"

Notes

  • Both words will have strictly different ASCII sums.
  • You can get the ASCII code for a single character using the ord function.
import unittest
def ascii_compare(word_a: str, word_b: str) -> str:
return "" # Put your code here!!!
class Test(unittest.TestCase):
def test_1(self):
self.assertEqual("man", ascii_compare("hey", "man"))
def test_2(self):
self.assertEqual("then", ascii_compare("majorly", "then"))
def test_3(self):
self.assertEqual("magic", ascii_compare("magic", "kingdom"))
def test_4(self):
self.assertEqual("bored", ascii_compare("bored", "shampoo"))
def test_5(self):
self.assertEqual("victory", ascii_compare("victory", "careless"))
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment