Posts

Escape from code illusion

Image
Sometimes we fall into illusion of performant code. It happened to me recently. This is what the case. Problem: Identify unique values in an array of values where all items appear exactly twice but one.  Super simple right? I wrote the following two different solutions . Solution 1:           var singleNumber = function(nums) {     return Object.keys(nums.reduce((acc, item) => {        if(typeof(acc[item]) !== 'undefined') {            delete acc[item];        } else {            acc[item] = item;        }         return acc;     },{}))[0]; }; Solution 2:       var singleNumber = function(nums) {         return nums.reduce((acc, item) => acc ^ item);      };   What your choice? mine was the first one. When I compare...

Problem solving with JS

Image
     HTML      CSS      td {        width: 2em;      }     JS let data = { rows: null, cols: null, matrix: null }; function generateString(n, m) { for(let i = 0, str=''; i arr.map(el => ` ${el} `).join(''); const rows = data => data.map(datum => ` ${row(datum)} `).join(''); return ` ${rows(matrix)} `; } document.getElementById('rows').addEventListener('change', ({target: {value}}) => { data.rows = parseInt(value) || 0; if ( data.cols ) { data.matrix = generateMatrix(data.rows, data.cols); } }); document.getElementById('cols').addEventListener('change', ({target: {value}}) => { data.cols = parseInt(value) || 0; if ( data.rows ) { data.matrix = generateMatrix(data.rows, data.cols); } }); document.getElementById('generate').addEventListener('click', e => { if(data.matrix) document.getElementById('r...

Not a Fun Game

Almost late. I thought checking mails won't take much time. But it is! Grab the bag, is Id there? Get the lunch box and ... where is my car key??? checking every where I couldn't recollect where I kept it. Checked the book shelf, TV stand, my last day pant and bathroom. OMG, why I am doing this mistake every time? Bigger house is convenient as well as discomfort. How am I going to locate the keys??? I stopped for a while. And started recollecting the moment when I entered home after my last drive. But with oscillation of various things in my mind, I couldn't focus. There is a meeting by 10 and I should go office at least by 9:50. "Why do you leave the key in dining table? Change the habit, keep the key all times in key holder" - It is Subha with car keys. She is right. This is not the first time she is talking about it. "OK", I haven't even stopped and thanked her. Ran to start the car. And after a few weeks, she was out. I was searching for...

Train Game

Choo choo... running behind friends on the street was fun. When I wanted to play the game with kids, they enjoyed it too. Adult next to us looking at weird. No one plays such games on street now a days. But try with your kids and neighboring kids and share how it energizes. We were 5 initially formed the chain. I was the Engine for first few games. I stood in the front and wait for the coaches to join. Sai krishna (my son) follows me every time, happily catches my shirt and said "I am behind you. Hey Engine, start start. Choo choo....". "Hey train wait...." - it was my daughter Danya sri catches Sai krishna shirt and formed next coach. We were riding on the street and all eyes on us. There comes our neighboring kid Sakthi. She follows Danya and now the train is little bigger. We were running on the street happily.  Then to make some fun in the middle, Engine started going fast and Danya missed to pick it up immediately. "Hey Engine, wait......