May the read be with you

“Successful people have libraries, the rest have big screen TVs.” - Jim Rohn

It’s time I finally acknowledge the importance of reading. I don’t give a damn to many things. My view changes when I become more ambitious, thanks to my partner-in-crime who is constantly on turbo mode. The only person in my life who wants to rule the world like Mojo Jojo. In turn, inspires me to become a Powerpuff girl. Oh man, I miss my cartoon. Alright, I’m being ridiculous and very much digressing.

Part of the influence comes from h3h3 and Jordan Peterson,

their brains got me hooked. Peterson’s ideology resonates with me, a hell lot. Then I chanced upon the epic infamous channel 4 interview in youtube; I LOL like shit.

Look, I found a good analysis of that epicness in this video:

The truth is,

Channel 4 epic interview embarrassed me as a female; it’s the damn time to give a damn understanding to patriarchy and women issues. Why only now? Because I’m freaking blessed to be born in a country, Singapore, where opportunities are based on merit. Is it because I’m Chinese? (This is a well thought singaporean infamous meme from my side 🤗). Highly possible. I am constantly surrounded by amazing people who are never once sexist; so lucky I become passive.

My first book is obviously Lean In: Women, Work, and the Will to Lead after my rigorous attempt to seek for the answer.

It was a good read until I see Michelle Obama’s take on the book – Sometimes that shit doesn't work. 👀 That. was. EXCITING. While you are at it, also google Ali wong’s take on lean in. Hi-la-ri-ous af.

In conclusion,

I don’t give many damns before. As I get older, my spider senses are tingling. These freaking ideologies and stereotypes will drown me one day, so it’s better I learn to float now.

I’ve carefully selected my next 3 books. Work related books because of the same reason; need to pour more answers into my brain. If you’re interested to be a better programmer, here’s a peek at my selection;

Now you’re convinced, I hope. Let me bring you through the “painful” steps that discouraged me from reading in the beginning.

  1. Download Kindle app on your phone.
  2. Buy the kindle version of your ebook on Amazon using 1 click button.

Literally 2 steps.

That’s all.

Day fucking 1 of #next3month movement where I do a little coding question, read a little book, draw a little stuff, video a little shit. All in all to be a little better everyday while waiting for Rick and Morty to come out.

P/S: I’m learning to write better. Be kind. 🌝


wreck-it-ralph

I just watched Ralph Breaks the Internet 😍

and I absolutely love the scene where Ralph is popping everywhere on the screen.

Ralph in CSS

Because it’s late and we need Ralph in CSS so I stole the amazing CSS pixel art by Jason Delia using box-shadow. We need to play with box-shadow again.

WRECK-IT-RALPH DEMO

Simple Popup Animation

Hide the original Ralph so the first Ralph is always at random position.

1
<div id="ralph"></div>

Duplicate Ralph using cloneNode and put it in body.

1
2
3
4
5
6
7
8
9
10
11
const body = document.getElementsByTagName("body")[0];
const ralph = document.getElementById("ralph");

function duplicateRalphAtRandomPosition() {
const ralphClone = ralph.cloneNode(true);
ralphClone.style.display = "block";
ralphClone.style.top = getRandomPercentage();
ralphClone.style.left = getRandomPercentage();
ralphClone.style.transform = `scale(${getRandomSize()})`;
body.appendChild(ralphClone);
}

Place cloned Ralph in random position with random size.

1
2
3
4
5
6
7
8
9
// Return 0% to 100%
function getRandomPercentage() {
return Math.random() * 101 + "%";
}

// Return 0.3 - 1
function getRandomSize() {
return Math.random() * 0.8 + 0.3;
}

Keep doing it every second until the Internet breaks using setInterval

1
2
3
4
// Duplicate Ralph every second yo
setInterval(function() {
duplicateRalphAtRandomPosition();
}, 1000);

P/S: Do not click on the ads.
P/P/S: Am I lazy if I post same content here and at dev.to


Convert it yourself

Day 9/100,

#100daysofcode is not easy for a person like me. By right I am supposed to be at day 14. By left, I am at day 9.

Oh and I got caught my google adsense by clicking on my own ads ( 🌝 so much for the cheap thrill and I wanted to know how much a click cost ). Apparently it is against their policies to do that. I feel so exposed knowing that everything I do is tracked. Big brother huh.

Yesterday,

I got this idea to do parallax scrolling website and I needed gif. And I decided to convert everything, images to gif and video all by myself instead of searching for online tools.

Note: background-attachement does not work in mobile. Sad. This works on desktop browser and not mobile browser. What is going on.

Convert it youself,

Using terminal and a few tools (imageMagick, ffmpeg), we can convert image to video or gif and vice versa. I focus on png because I needed the gif and video to be transparent.

Basic Steps,

  1. Create some images and named them in sequence. 1.png, 2.png …
  2. Open terminal and run some commands with ffmpeg or convert
  3. Voila, you have a GIF.

Learn more,

If you are interested to know how:
https://github.com/linxea/convert-it-yourself

Retrospective,

I really adore scrum at work. I feel that scrum can be minified and applied to life. My sprint would be a day long with multiple refinements done during travelling on public transport. At the end of the day, I am able to complete with this post, a short review (the demo you see) and retrospective on the good, the bad and the ugly.

Quickly,

The Good:

  • 100daysofcode challenge challenges my concept of time.
  • I see myself getting more proactive and responsive to things.
  • I set reasonable goals although I still can’t achieve 100%.

The Bad:

  • At work, I think of my work.
  • While doing my personal work, I think of work. wtf.

The Ugly:

  • Brain does weird thing at midnight. I started a patreon without any content and promises. What the fuck was I thinking. But I like how I do thing without hesitation now. Do and regret is much better than no doing and not regretting.

On a side note,

I am so proud of the movie Crazy Rich Asians. Good plot, good acting, good location (my hometown Singapore!) The first hollywood featuring all asian casts and the movie is filmed in Singapore with many singaporeans! <3 I love the humour and credit scene animation.

Coming soon,


Find all combinations of IP address from a string of numbers

Q: Find all combinations of IP address from a string of numbers.

FYI,

IP Address

  1. We are looking at IPv4 which is 32 bit long, represented by 4 numbers ranging from - to 255 in 4 dots
  2. 0.0.0.0 (in number) === 00000000.00000000.00000000.00000000 (in bits) = 8bits.8bits.8bits.8bits = 1byte.1byte.1byte.1byte
  3. 8bit = 0 to 255 = 256 numbers in each part
  4. IP address ranges from 0.0.0.0 to 255.255.255.255
  5. 32 bits IP address means 256^4 = 4.3 billion IP addresses in the world. woah
  6. IPv6 has 128bits which means 340 trillion trillion trillion IP addresses. woah woah

Bit

  1. Bit = Binary Digit
  2. 1 bit = smallest unit of data in a computer
  3. 8 bits = 1 byte = 00000000 (in bits, 8 zeros, 2^8 numbers = 256, 0 - 255)
  4. n bits = 2^n ways the bits can be arranged = 0…n-1 (in binary) = 0 - 2^n -1 (in number)
  5. unsigned number means only positive integers; 0 - 2^n -1
  6. signed number means positive and negative integers; -2^n to 2^n - 1

Sample example,

Given a string: ‘1 2 3 4 5’,
Number of combinations:
1.2.3.4
1.2.3.45
1.2.3.5
1.2.34.5
1.2.4.5
1.23.4.5
1.3.4.5
12.3.4.5
2.3.4.5

Find pattern

Finding…

Solution

1
Solutioning...

NOTE: THIS IS AN ONGOING THINKING PROCESS. CHECK BACK LATER.

The End,

Day 8/100, dynamic programming has been fun. Time to enter the maze of graph.


Painting fence problem [Part 1 - Non Optimized]

-> Day7/100

Q: Given a fence with n posts and k colors, find out the number of ways of painting the fence such that at most 2 adjacent posts have the same color.

I found 2 approaches to tackle this problem.

1: Do it recursively

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def findWaysUtil(postCount, arrayOfColors, string):
# Base case: There is a way with this combination string
if postCount == 0:
return 1

sum = 0

for i in range(len(arrayOfColors)):
# If current color is the same as the last color of the string
# check if string contains two consecutive colors already,
# because we can only have maximum 2 colors side by side
isThreeColorsInARow = len(
string) > 1 and string[-2:-1] == string[-1:] == str(
arrayOfColors[i])

if not isThreeColorsInARow:
sum += findWaysUtil(postCount - 1, arrayOfColors,
string + str(arrayOfColors[i]))

return sum


def findWays(postCount, numberOfColors):
# Check for invalid number of post
if postCount == 0:
return 0

# We will use number to represent colors
arrayOfColors = [i for i in range(numberOfColors)]

# We will pass down the combinaton of colors stored in a string
EMPTY_STRING = ''

return findWaysUtil(postCount, arrayOfColors, EMPTY_STRING)


print findWays(0, 3) # 0
print findWays(1, 3) # 3
print findWays(2, 3) # 9
print findWays(3, 3) # 24

2: Do it using pattern

We need to start with a base case of just 1 post. I am lazy to draw so we will use [ ] to represent a post. We should test all cases of n and k in numbers, so we will focus at the base cases of n, and keep k a constant variable.

Base case, test with 1 post where n = 1 and k colors.

1
2
3
4
5
[], red black

if n = 1 and k = 2,
[] can be [red] or [black]
numberOfWays = 2

Ok, now we test with 2 posts.

1
2
3
4
5
[][], red black

if n = 2 and k = 2
[][] can be [red][red], [black][black], [red][black], [black][red]
numberOfWays = 4

We split into 2 cases; check if [Post a] and [Post b] are the same or different color:

  1. Both posts are the same color: [red][red], [black][black]
    sameColorAdjacentPost
    = (k colors to choose from) x (1 way to follow previous post color)
    = k * 1 = k = 2 * 1 = 2

  2. Both posts are of different color: [red][black], [black][red]
    differentColorAdjacentPosts
    = (k colors to choose from) x (don’t choose the previous color means k - 1 ways)
    = k * (k - 1) = 2 * 1 = 2

numberOfWays = sameColorAdjacentPost + differentColorAdjacentPosts = k + k(k - 1)

Our last base case:
3 posts with n = 3

1
2
3
4
5
6
7
8
9
10
11
[][][], red black

if n = 3 and k
[][][] can be

[red][black][black]
[red][red][black]
[red][black][red]
[black][red][black]
[black][red][red]
[black][black][red]

differentColorAdjacentPosts
= [post a][post b][post c]
= [All the number of ways from previous n which has [post a][post b]][just choose color that is not in post b which is k - 1 ways]
= (k + k(k-1)) * (k-1 ways since it cannot be same as previous color only)

sameColorAdjacentPost
= [post a][post b][post c]
= [k colors][k - 1 colors][post c to follow post b so it has 1 way only]
= [red][black][black] = k * (k-1)

numberOfWays
= sameColorAdjacentPost + differentColorAdjacentPosts

Until here we are able to see a pattern forming. Thankfully.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
if n == 1:
numberOfWays = k

if n == 2:
sameColorAdjacentPost
= k * (1 way to be same as the post before)
= k

differentColorAdjacentPosts
= k * (k - 1 ways to be different from the post before)
= k(k - 1)

numberOfWays
= sameColorAdjacentPost + differentColorAdjacentPosts
= k + k(k - 1)

if n == 3:
sameColorAdjacentPost
= k * (k - 1) * (1 way to be same as the post before)
= k(k - 1) that looks like differentColorAdjacentPosts in n=2
# this is because we cannot have 3 of the same color in a row
# know the previous 2 colors are different, we can expand on it

differentColorAdjacentPosts =
= (all the number of ways in n=2) (k - 1)
= (k + k(k - 1))(k - 1)
= (numberOfWays in n=2)(k - 1)
# if you know all the ways for 2 posts, just * (k-1) to all to get different color from the post before

numberOfWays
= sameColorAdjacentPost + differentColorAdjacentPosts
= k(k - 1) + (k + k(k - 1))(k - 1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
NUMBER_OF_WAY_0 = 0


def findWaysUtil(n, k):
if n <= 0:
return (NUMBER_OF_WAY_0, NUMBER_OF_WAY_0)

elif n == 1:
return (NUMBER_OF_WAY_0, k)

elif n == 2:
numberOfWaysCurrentPostAndPreviousPostToBeSameColor = k
numberOfWaysCurrentPostAndPreviousPostToBeDifferentColor = k * (k - 1)

else:
# for n >= 3, at every point of time, we are only looking at current post
# adding on top of the number of ways from (post - 1)

result = findWaysUtil(n - 1, k)
numberOfWaysCurrentPostAndPreviousPostToBeSameColor = result[0]
numberOfWaysCurrentPostAndPreviousPostToBeDifferentColor = result[1] * (
k - 1)

totalNumberOfWays = numberOfWaysCurrentPostAndPreviousPostToBeSameColor + numberOfWaysCurrentPostAndPreviousPostToBeDifferentColor

return (numberOfWaysCurrentPostAndPreviousPostToBeDifferentColor,
totalNumberOfWays)


def findWays(n, k):
if n <= 0:
return NUMBER_OF_WAY_0

# findWaysUtil returns a tuple (number of ways with different colors for last and second last post, total number of ways)
result = findWaysUtil(n, k)
totalNumberOfWays = result[1]

return totalNumberOfWays


print findWays(0, 3) # 0
print findWays(1, 3) # 3
print findWays(2, 3) # 9
print findWays(3, 3) # 24

Time / space complexity is terrible. Exponential.
We will use another post to optimize it. I promise.

p/s: Added a level on blackbox. linxea.github.io/blackbox


6/100 -> Tick fucking tock

How I felt in these 6 days of 100daysofcode…

#Coding question of the day,

Continue from previous question …

Q: Given a set of coins (1,2,3) and a value (5). Find out the list of combinations with the set of coins that sum up to the given value.

1
2
3
Given a set of coins [1,2,3] and value 5, return the list of combinations in an array of string.

Combinations: ['23', '122', '113', '1112', '11111']

Soluton 1: Naive

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def combination(coins, value):
if len(coins) == 0 or value < 0:
return []

if coins[0] - value == 0:
return [str(coins[0])]

permutationOfCoins = combination(coins, value - coins[0])

for i in range(len(permutationOfCoins)):
permutationOfCoins[i] = str(coins[0]) + permutationOfCoins[i]

return combination(coins[1:], value) + permutationOfCoins


coins, value = [1, 2, 3], 5
print combination(coins, value)

Result:

1
['23', '122', '113', '1112', '11111']

Soluton 2: Better

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def combination(coins, value, memo):
if len(coins) == 0 or value < 0:
return []

if coins[0] - value == 0:
return [str(coins[0])]

sortedCoinsWithValue = (''.join(map(str, sorted(coins))), value - coins[0])

if sortedCoinsWithValue not in memo:
memo[sortedCoinsWithValue] = combination(coins, value - coins[0], memo)

for i in range(len(memo[sortedCoinsWithValue])):
memo[sortedCoinsWithValue][i] = str(
coins[0]) + memo[sortedCoinsWithValue][i]

return combination(coins[1:], value, memo) + memo[sortedCoinsWithValue]


def combinationUtil(coins, value):
memo = {}
return combination(coins, value, memo)


coins, value = [1, 2, 3], 5
print combinationUtil(coins, value)

Result:

1
['23', '122', '113', '1112', '11111']

With that we can easily find the least number of coins that sum up to the value.

BONUS QUESTION!

Q: Given a set of coins, find the least number of coins that sum up to a given value. Return 0 if there is none.

1
2
3
4
Given coins [1,2,3] and value = 5,
Answer: 2

Combinations: ['23', '122', '113', '1112', '11111']
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def combination(coins, value, memo):
if len(coins) == 0 or value < 0:
return []

if coins[0] - value == 0:
return [str(coins[0])]

sortedCoinsWithValue = (''.join(map(str, sorted(coins))), value - coins[0])

if sortedCoinsWithValue not in memo:
memo[sortedCoinsWithValue] = combination(coins, value - coins[0], memo)

for i in range(len(memo[sortedCoinsWithValue])):
memo[sortedCoinsWithValue][i] = str(
coins[0]) + memo[sortedCoinsWithValue][i]

return combination(coins[1:], value, memo) + memo[sortedCoinsWithValue]


def combinationUtil(coins, value):
sortedSetOfCoins = sorted(coins) # so our results will be sorted too ^^
memo = {}

results = combination(sortedSetOfCoins, value, memo)
leastNumberOfCoins = results[0]
return len(leastNumberOfCoins)


coins, value = [3, 2, 1], 5
print combinationUtil(coins, value)

Result:

1
2

#End,

Oh my gawd, I am enjoying doing coding question more than I expected? Much more than when I was in school. What the heck. Why Java, why so much paper, why dry content.