Skip to content

Instantly share code, notes, and snippets.

@BonfaceKilz
Last active March 18, 2021 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BonfaceKilz/26e36c39a4a0d0dcaa71e2edc3b4b5fa to your computer and use it in GitHub Desktop.
Save BonfaceKilz/26e36c39a4a0d0dcaa71e2edc3b4b5fa to your computer and use it in GitHub Desktop.
000. Monchoka! Weekly Algos by your neighbourly scheme-r ;)

Prelude

Hi folks! This one took a while to post; but nevertheless, here we are!

Last week, Perserverance(a.k.a Percy) landed on Mars; which saw Linux on Mars! One small win(?) for Linux(?) \o/ \o/

Earlier this week, I picked up (again) "The Philosophy of Software Design by John Ousterhout" and I found myself nerding about it with some fellas at the office-- poor souls ;)!. The particularly interesting thing was the book's discussion of what our role is (as SWEs) wrt work; with an opinionated view that it all boils down to tackling "complexity". Without getting into much detail, I particularly found the books discussion of "causes of complexity" interesting: Dependencies and obscurity. For anyone getting into the field, I'd strongly recommend that read(albeit pore through it with a critical eye).

Last Week's Challenge

Issue 1: https://is.gd/HK0jFF

Mailing List discussion: https://is.gd/xrHZmY

If you get the bandwidth, hack at the problems at the submission links above-- It's a nice way to get comments on your code \m/\m/.

This Week's Challenge

Source: https://is.gd/HwTL9E

Given an integer as a function, test to see if it's a palindrome

Constraints: You may not cast the number to a string!

Examples:
Input: n = 9229
Output: True

input: n = 1234
Output: False

Post your answers here as comments.

@ianmuchina
Copy link

Solution in python

class Solution:
    def isPalindrome(self, x: int) -> bool:
        # reverse then compare
        initial = x

        rev = 0
        while x != 0:

            # Get the number in ones place
            #   eg: 123 % 10 = 3
            n = x % 10

            # Make space in rev
            rev = rev * 10
            # add n in the new space
            rev = rev + n

            # Delete the number in ones place
            #   eg: 123 / 10 = 12
            x =  math.trunc(x / 10)
            
        if initial == rev:
            return True
        else:
            return False

Reversing an integer mathematically

@kodesoul
Copy link

kodesoul commented Feb 24, 2021

Solution in rust:

fn rev_int(num: i32) -> i32 {
    let mut num = num;
    let mut rev = 0;

    while num != 0  {
        rev *= 10;
        rev += num % 10;
        num /= 10;
    };
    rev
}

fn is_palindrome(n: i32) -> bool {
    n == rev_int(n)
}

@BonfaceKilz
Copy link
Author

BonfaceKilz commented Feb 25, 2021 via email

@BonfaceKilz
Copy link
Author

BonfaceKilz commented Feb 25, 2021 via email

@DMGithinji
Copy link

DMGithinji commented Mar 1, 2021

`
function isPalindrome(x: number): boolean {
let rev = 0;
let copy = x;

    while(copy > 0) {
        const rem = copy%10;
        rev = rem + (rev * 10);
        copy = Math.floor(copy/10);
    }
    return rev === x;

}
`

@BonfaceKilz
Copy link
Author

BonfaceKilz commented Mar 1, 2021 via email

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