AmitKumarDas / Decisions

Apache License 2.0
10 stars 3 forks source link

fun with programming #272

Open AmitKumarDas opened 3 years ago

AmitKumarDas commented 3 years ago

Number with No consecutive 1s in binary representation

If Ten Tea Less Buns Equals Egg
Then Breaking Bun Chain Is Possible
if (t&(t<<1)) == 0
AmitKumarDas commented 3 years ago

2 Sum problem with sorted inputs solved via 2 Pointers

AmitKumarDas commented 3 years ago

All numbers upto n bits

1<<n
AmitKumarDas commented 3 years ago

A man, a plan, a canal: Panama is a valid Palindrome

Panana Palindrome
while(i<j){
  while(i<j && arr[i].is_not_digit) i++
  while(i<j && arr[j].is_not_digit) j--
  if lower(arr[i]) != lower(arr[j]) return false
  i++
  j--
}
return true
AmitKumarDas commented 3 years ago

strStr - search needle from haystack

for (i=0;;i++)
  for(j=0;;j++)
    if j==len(needle) return i
    if i+j==len(needle)+len(haystack) return -1
    if needle[j] != haystack[i+j] break

Str Str - First String Occurrence

needle[j] == haystack[i+j] 
Needle Jr. Hijacked
AmitKumarDas commented 3 years ago

reverse chars - loop till mid based swaps


reverse(char[] s, int begin, int end) {
  for (int i = 0; i < (end - begin) / 2; i++) {
    char temp = s[begin + i]; // begin with I/Me
    s[begin + i] = s[end - i - 1]; // end without Me & Buffet
    s[end - i - 1] = temp;
  }
}

- reverse chair - loop till mid - semi circular chair
- reverse chair - MIDDLE - Half Idle

- reverse chair - Begins with Me
- reverse chair - Ends without Me & Buffet
AmitKumarDas commented 3 years ago

Move On If Whitspace

int i = 0, j = str.length()
while (i < n && Character.isWhitespace(str.charAt(i))) i++;
AmitKumarDas commented 3 years ago

For is Go's While

sum := 1
for sum < 1000 {
  sum += sum
}
AmitKumarDas commented 3 years ago

When 1000 turns 1024

func main() {
  sum := 1
  for sum < 1000 {
    sum += sum
  }
  fmt.Println(sum) // 1024
}
AmitKumarDas commented 3 years ago

Is String A Number

return isNumeric && i == n
while (i < n && Character.isWhitespace(s.charAt(i))) i++;
if (i < n && (s.charAt(i) == '+' || s.charAt(i) == '-')) i++;

boolean isNumeric = false;
while (i < n && Character.isDigit(s.charAt(i))) {
  i++;
  isNumeric = true;
}

if (i < n && s.charAt(i) == '.') {
  i++;
  while (i < n && Character.isDigit(s.charAt(i))) {
    i++;
    isNumeric = true;
  }
}

if (isNumeric && i < n && s.charAt(i) == 'e') {
  i++;
  isNumeric = false;
  if (i < n && (s.charAt(i) == '+' || s.charAt(i) == '-')) i++;
  while (i < n && Character.isDigit(s.charAt(i))) {
    i++;
    isNumeric = true;
  }
}

while (i < n && Character.isWhitespace(s.charAt(i))) i++;
return isNumeric && i == n;
AmitKumarDas commented 3 years ago

Rune

str := "GOLANG"
runes := []rune(str)

var result []int
for i := 0; i < len(runes); i++ {
  result = append(result, int(runes[i]))
}

fmt.Println(result) // [71 79 76 65 78 71]
  // Example - 2
  s := "GOLANG"
  for _, r := range s {
    fmt.Printf("%c - %d\n", r, r)
  }
G - 71
O - 79
L - 76
A - 65
N - 78
G - 71