'전체 글'에 해당되는 글 3건

  1. 2019.08.29 Check Permutation
  2. 2019.08.29 Is Unique?
  3. 2019.08.19 Python String Formatting
JavaScript2019.08.29 20:30

Given two strings, write a method to decide if one is a permutation of the other.

 


var checkPermute = function(stringOne, stringTwo) {
  // if different lengths, return false
  if (stringOne.length !== stringTwo.length) {
    return false;
  // else sort and compare 
  // (doesnt matter how it's sorted, as long as it's sorted the same way)
  } else {
    var sortedStringOne = stringOne.split('').sort().join('');
    var sortedStringTwo = stringTwo.split('').sort().join('');
    return sortedStringOne === sortedStringTwo;
  }
};

// Tests
console.log(checkPermute('aba', 'aab'), true);
console.log(checkPermute('aba', 'aaba'), false);
console.log(checkPermute('aba', 'aa'), false);

Posted by 섹개

댓글을 달아 주세요

JavaScript2019.08.29 20:03

Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures?

 


var allUniqueChars = function(string) {
  
  
  // O(n^2) approach, no additional data structures used
  // for each character, check remaining characters for duplicates
  for (var i = 0; i < string.length; i++) {
    for (var j = i + 1; j < string.length; j++) {
      if (string[i] === string[j]) {
        return false; // if match, return false
      }
    }
  }
  return true; // if no match, return true
};

/* TESTS */
// log some tests here

console.log(allUniqueChars("abcd")); // true
console.log(allUniqueChars("aabbcd")); // false

Posted by 섹개

댓글을 달아 주세요

Python2019.08.19 00:25

Given an integer, n, print the following values for each integer i from to n:

 

  1. Decimal
  2. Octal
  3. Hexadecimal (capitalized)
  4. Binary

 

The four values must be printed on a single line in the order specified above for each i from 1 to n. Each value should be space-padded to match the width of the binary value of n.

 

Input Format

A single integer denoting n.

 

Constraints

 

Output Format

Print n lines where each line i (in the range 1 <= i <= n ) contains the respective decimal, octal, capitalized hexadecimal, and binary values of i.  Each printed value must be formatted to the width of the binary value of n.

 

Sample Input

17

Sample Output

    1     1     1     1
    2     2     2    10
    3     3     3    11
    4     4     4   100
    5     5     5   101
    6     6     6   110
    7     7     7   111
    8    10     8  1000
    9    11     9  1001
   10    12     A  1010
   11    13     B  1011
   12    14     C  1100
   13    15     D  1101
   14    16     E  1110
   15    17     F  1111
   16    20    10 10000
   17    21    11 10001     

 

Solution

def print_formatted(num):
  # your code goes here

  for a in range(num):
    a += 1
    w = len(str(bin(num))) - 2
    print("{val:{width}d} {val:{width}o} {val:{width}X} {val:{width}b}".format(width=w, val=a))

if __name__ == '__main__':
  n = int(input())
  print_formatted(n)

Posted by 섹개

댓글을 달아 주세요