diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..a8a0dce --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.bin diff --git a/bench.sh b/bench.sh new file mode 100755 index 0000000..0866283 --- /dev/null +++ b/bench.sh @@ -0,0 +1,22 @@ +#!/bin/bash +BENCH="hyperfine -i -N -w 10 " + +for dir in *; do + if [ -d "$dir" ]; then + # Compile command with the name + COMPILE="mojo build ${dir}.mojo -o ${dir}.bin" + # Generate commands to run + COMMANDS=("python ${dir}.py") + COMMANDS+=("mojo ${dir}.mojo") + COMMANDS+=(${dir}.bin) + cd "$dir" + echo + echo "Benchmarking $dir" + echo "======================" + # Compile mojo first + $COMPILE + # Run Benchmark with all the commands + $BENCH "${COMMANDS[@]}" + cd .. + fi +done diff --git a/problem-1/problem-1.mojo b/problem-1/problem-1.mojo old mode 100644 new mode 100755 index 040e397..2b013e2 --- a/problem-1/problem-1.mojo +++ b/problem-1/problem-1.mojo @@ -1,22 +1,15 @@ -from python import Python +fn lengthOfLastWord(enterword: String) -> Int: + var count: Int = 0 -def lengthOfLastWord(enterword: String): - - py = Python.import_module("builtins") - - count = 0 - for i in range(len(enterword)-1, -1, -1): + for i in range(len(enterword) - 1, -1, -1): if enterword[i] != " ": count += 1 elif count > 0: break - - print("Length of the last word:") - print(count) - -def main(): - input_string = "Hello World" - lengthOfLastWord(input_string) + return count +fn main(): + for _ in range(100000): + _ = lengthOfLastWord("Hello World") diff --git a/problem-1/problem-1.py b/problem-1/problem-1.py old mode 100644 new mode 100755 index 41ea996..c72a76a --- a/problem-1/problem-1.py +++ b/problem-1/problem-1.py @@ -1,5 +1,4 @@ -def lengthOfLastWord(enterword): - +def length_of_last_word(enterword): count = 0 for i in range(len(enterword)-1, -1, -1): @@ -8,10 +7,12 @@ def lengthOfLastWord(enterword): elif count > 0: break - print("Length of last word", count) + return count + def main(): - input_string = "Hello World" - result = lengthOfLastWord(input_string) - -main() + [length_of_last_word("Hello World") for _ in range(100000)] + + +if __name__ == "__main__": + main() diff --git a/problem-2/problem-2.mojo b/problem-2/problem-2.mojo old mode 100644 new mode 100755 index 932bbc1..cde7d03 --- a/problem-2/problem-2.mojo +++ b/problem-2/problem-2.mojo @@ -1,32 +1,22 @@ -from python import Python - -def checkRecord(attendance_record: String): - py = Python.import_module("builtins") - attendance_record_str = attendance_record - - absent_count = 0 - late_count = 0 +fn checkRecord(attendance_record: String) -> Bool: + var attendance_record_str = attendance_record + var absent_count = 0 + var late_count = 0 for i in range(len(attendance_record_str)): - - if attendance_record_str[i] == 'A': + if attendance_record_str[i] == "A": absent_count += 1 late_count = 0 - elif attendance_record_str[i] == 'L': + elif attendance_record_str[i] == "L": late_count += 1 else: late_count = 0 - if late_count >= 3 or absent_count >= 2: - print("The attendance record is not acceptable.") return False - print("The attendance record is acceptable.") return True -def main(): - checkRecord("PAALP") - - - +fn main(): + for _ in range(100000): + _ = checkRecord("PAALP") diff --git a/problem-2/problem-2.py b/problem-2/problem-2.py old mode 100644 new mode 100755 index 1ea19c8..94ee66f --- a/problem-2/problem-2.py +++ b/problem-2/problem-2.py @@ -1,12 +1,9 @@ -def checkRecord(attendance_record): - +def check_record(attendance_record): attendance_record_str = str(attendance_record) - absent_count = 0 late_count = 0 for i in range(len(attendance_record_str)): - if attendance_record_str[i] == 'A': absent_count += 1 late_count = 0 @@ -14,16 +11,17 @@ def checkRecord(attendance_record): late_count += 1 else: late_count = 0 - if late_count >= 3 or absent_count >= 2: - print("The attendance record is not acceptable.") + # print("The attendance record is not acceptable.") return False - print("The attendance record is acceptable.") + # print("The attendance record is acceptable.") return True -# Main function + def main(): - checkRecord("PAALP") - -main() + [check_record("PPALLP") for _ in range(100000)] + + +if __name__ == "__main__": + main() diff --git a/problem-3/problem-3.mojo b/problem-3/problem-3.mojo old mode 100644 new mode 100755 index 9b59c3e..c70f650 --- a/problem-3/problem-3.mojo +++ b/problem-3/problem-3.mojo @@ -1,25 +1,17 @@ -from python import Python +fn makeGood(s: String) -> String: + var result = str("") + var i = 0 -def makeGood(s: String): - py = Python.import_module("builtins") - result = str("") - - i = 0 while i < len(s): - - if i < len(s) - 1 and py.abs(ord(s[i]) - ord(s[i + 1])) == 32: - + if i < len(s) - 1 and abs(ord(s[i]) - ord(s[i + 1])) == 32: i += 2 else: - result = result + s[i] i += 1 - print(result) + return result + -def main(): - - input_str = "leEeetcode" - makeGood(input_str) - - #print("After making the string good:", result) +fn main(): + for _ in range(1000000): + _ = makeGood("leEeetcode") diff --git a/problem-3/problem-3.py b/problem-3/problem-3.py old mode 100644 new mode 100755 index de9d28f..944f38a --- a/problem-3/problem-3.py +++ b/problem-3/problem-3.py @@ -1,23 +1,20 @@ -def makeGood(s): +def make_good(s): result = "" - i = 0 + while i < len(s): - if i < len(s) - 1 and abs(ord(s[i]) - ord(s[i + 1])) == 32: - i += 2 else: - result += s[i] i += 1 return result + def main(): - - input_str = "leEeetcode" - output_str = makeGood(input_str) - print(output_str) + [make_good("leEeetcode") for _ in range(1000000)] + -main() +if __name__ == "__main__": + main() diff --git a/problem-4/problem-4.mojo b/problem-4/problem-4.mojo old mode 100644 new mode 100755 index 5f4632a..4b5e5c0 --- a/problem-4/problem-4.mojo +++ b/problem-4/problem-4.mojo @@ -1,36 +1,22 @@ -from python import Python +fn detectCapitalUse(word: String) -> Bool: + var uinput = word + var number_of_capital_letters = 0 -def detectCapitalUse(word: String): - py = Python.import_module("builtins") - - uinput = word - - number_of_capital_letters = 0 for i in range(len(uinput)): - letter = uinput[i] + var letter = uinput[i] if ord(letter) < 97: number_of_capital_letters += 1 - number_of_small_letters = len(uinput) - number_of_capital_letters + var number_of_small_letters = len(uinput) - number_of_capital_letters - if number_of_capital_letters == len(uinput) or number_of_small_letters == len(uinput) or (ord(word[0]) < 97 and number_of_capital_letters == 1): + if number_of_capital_letters == len(uinput) + or number_of_small_letters == len(uinput) + or (ord(word[0]) < 97 and number_of_capital_letters == 1): return True else: return False -def main(): - result = detectCapitalUse("USA") - print("Result:") - print(result) - - - - - - - - - - - +fn main(): + for _ in range(1000000): + _ = detectCapitalUse("USA") diff --git a/problem-4/problem-4.py b/problem-4/problem-4.py old mode 100644 new mode 100755 index fab857d..707a82d --- a/problem-4/problem-4.py +++ b/problem-4/problem-4.py @@ -1,10 +1,7 @@ - -def detectCapitalUse(word): - - +def detect_capital_use(word): uinput = str(word) - number_of_capital_letters = 0 + for i in range(len(uinput)): letter = uinput[i] if ord(letter) < 97: @@ -12,13 +9,17 @@ def detectCapitalUse(word): number_of_small_letters = len(uinput) - number_of_capital_letters - if number_of_capital_letters == len(uinput) or number_of_small_letters == len(uinput) or (ord(word[0]) < 97 and number_of_capital_letters == 1): + if (number_of_capital_letters == len(uinput) + or number_of_small_letters == len(uinput) + or (ord(word[0]) < 97 and number_of_capital_letters == 1)): return True else: return False + def main(): - result = detectCapitalUse("USA") - print("Result:") - print(result) -main() \ No newline at end of file + [detect_capital_use("USA") for _ in range(1000000)] + + +if __name__ == "__main__": + main() diff --git a/problem-5/problem-5.mojo b/problem-5/problem-5.mojo old mode 100644 new mode 100755 index 9a34d25..16b4dd0 --- a/problem-5/problem-5.mojo +++ b/problem-5/problem-5.mojo @@ -1,14 +1,13 @@ -from python import Python +fn findTheDifference(s:String, t:String) -> String: + var result = str("") -def findTheDifference(s:String, t:String): - py = Python.import_module("builtins") - for i in range(len(t)): if s[i] != t[i]: result = t[i] - print("The difference is:") - print(result) - break -def main(): - findTheDifference("abcd", "abced") + return result + + +fn main(): + for _ in range(100000): + _ = findTheDifference("abcd", "abced") diff --git a/problem-5/problem-5.py b/problem-5/problem-5.py old mode 100644 new mode 100755 index 90b29dc..088fe64 --- a/problem-5/problem-5.py +++ b/problem-5/problem-5.py @@ -1,12 +1,15 @@ -def findTheDifference(s, t): - +def find_the_difference(s, t): for i in range(len(t)): if s[i] != t[i]: result = t[i] break - print("The difference is:", result) + return result + def main(): - findTheDifference("abcd", "abced") -main() + [find_the_difference("abcd", "abced") for _ in range(100000)] + + +if __name__ == "__main__": + main()