Skip to main content

Python vs. JavaScript

FACTORIALIZE A NUMBER

If the integer is represented with the letter n, a factorial is the product of all positive integers less than or equal to n.

Factorials are often represented with the shorthand notation n!.

For example: 5! = 1 * 2 * 3 * 4 * 5 = 120.

Re­turn the fac­to­ri­al of the pro­vid­ed in­te­ger.

factorialize(5) should return 120.
factorialize(10) should return 3628800.
factorialize(20) should return 2432902008176640000.
factorialize(0) should return 1.

JAVASCRIPT

    function factorialize (n) {
      if (n < 0) {
        return undefined
      } else if (n === 0) {
        return 1
      } else {
        return (n * factorialize(n - 1))
      }
    }
    
PYTHON

    def factorialize(n):
        if n < 0:
            return None
        elif n == 0:
            return 1
        else:
            return n * factorialize(n - 1)
    

PALINDROME CHECKER

A palin­drome is a word or sen­tence that’s spelled the same way both for­ward and back­ward, ig­nor­ing punc­tu­a­tion, case, and spac­ing.

Return true if the given string is a palindrome. Otherwise, return false.

palindrome(‘eye’) should return true.
palindrome(‘_eye’) should return true.
palindrome(‘race car’) should return true.
palindrome(‘not a palindrome’) should return false.
palindrome(‘A man, a plan, a canal. Panama’) should return true.
palindrome(‘never odd or even’) should return true.
palindrome(‘My age is 0, 0 si ega ym.’) should return true.
palindrome(‘1 eye for of 1 eye.’) should return false.
palindrome(‘0_0 (: /-\ :) 0-0’) should return true.
palindrome(‘five|\_/|four’) should return false.

JAVASCRIPT

    function palindrome (stringInput) {
      /* Remove all non-alphanumeric characters
      from string and make lowercase */
      var stringForward = stringInput.toLowerCase().replace(/[^0-9a-z]/gi, '')
      // Create variable of string reversed
      var stringReversed = stringForward.split('').reverse().join('')
      // Check if string and reversed string are same
      if (stringForward === stringReversed) {
        return true
      } else {
        return false
      }
    }
    
PYTHON

    import re

    def palindrome(string_input):
        # Remove all non-alphanumeric characters
        # from string and make lowercase 
        string_forward = (re.sub('[^A-Za-z0-9]', '', string_input).lower())
        # Create variable of string reversed
        # string_reversed = ''.join(reversed(string_forward))
        string_reversed = string_forward[::-1]
        # Check if string and reversed string are same
        if string_forward == string_reversed:
            return True
        else:
            return False
    

OpenSource

Write a pro­gram that se­quen­tial­ly out­puts in­te­gers in a range, but on some con­di­tions prints a string in­stead:

  • when the integer is a multiple of 3 print Open instead of the number;
  • when it is a multiple of 7 print Source instead of the number;
  • when it is a multiple of both 3 and 7 print OpenSource instead of the number.
JAVASCRIPT

    function openSource (low, high) {
      for (let n = low; n < high; n++) {
        if (n % 3 === 0 && n % 7 === 0) {
          console.log('OpenSource');
        } else if (n % 3 === 0) {
          console.log('Open');
        } else if (n % 7 === 0) {
          console.log('Source');
        } else {
          console.log(n);
        }
      }
    }
    
PYTHON

    def open_source(low, high):
        for number in range(low, high):
            if number % 3 == 0 and number % 7 == 0:
                print('OpenSource')
            elif number % 3 == 0:
                print('Open')
            elif number % 7 == 0:
                print('Source')
            else:
                print(number)
    

SUM OF INTEGERS ONLY

Write a func­tion that takes a list of strings and re­turns the sum of the list items that rep­re­sent an in­te­ger (skip­ping all oth­er chrac­ter­s).

sum_of_list([‘e’, ‘g’, ‘343e’, ‘200’, ‘-2’, ‘1.1’, ‘3’, ‘4’, ‘5’, ‘@’]) should return 210.

JAVASCRIPT

    const add = (a, b) => a + b

    function sumOfList (listOfStrings) {
      const listOfDigits = []

      for (let i = 0; i < listOfStrings.length; i++) {
        if (listOfStrings[i].indexOf('.') === -1 && listOfStrings[i].match(/^[-0-9]+$/) != null) {
          const number = parseInt(listOfStrings[i]) || 0
          listOfDigits.push(number)
        }
      }
      return listOfDigits.reduce(add)
    }
    
PYTHON

    def sum_of_list(list_of_strings):
        list_of_digits = []
        # sum_of_digits = 0 # alternate

        for item in list_of_strings:
            try:
                int(item)
            except ValueError:
                pass
            else:
                list_of_digits.append(int(item))
                # sum_of_digits += int(item) # alternate
        return sum(list_of_digits)
        # return sum_of_digits # alternate