Skip to main content

Posts

C Quiz

 This is more of a promotional post rather than a coding tip. I have few online quiz on various topics of C, C++, Java and Data structures. Recently I updated C Quiz and C coding questions page. You can just visit the site and take a random 10 question quiz on these topics. 

Change Grub timeout duration

 Grub is the most commonly used boot loader in Linux systems.         Grub : A program that calls a Unix/Linux operating system into memory. Officially GNU GRUB, GRUB is a popular boot loader due to its flexibility and configuration capabilities, allowing changes to be made at boot time and support for boot images from the network.  Quite often you don't see the grub menu at all. And under most circumstances it is OK. But if you have some problems and you have to go to recovery mode, you have to see the menu and select an OS from the menu.  So you love to see this grub menu when you boot your system and you are unable to see it. The culprit is the GRUB_TIMEOUT. If this value is set to 0, then you can't see the menu.  The correct method to change its setting is through the file /etc/default/grub   sudo vi /etc/default/grub $ cat /etc/default/grub # If you change this file, run 'update-grub' afterwards to update # /boot/grub/grub.c...

Financial Calc app gitted

  To publish or not to publish, that is the question said me. I had this not a great app, but maybe useful app. For Indian users. Given your Fixed Deposit or Recurring Deposit, it would calculate the final amount in various configuration.  That way, you could check whether the app used by banks is correct or not. But seriously, I had added interest rates of some banks and compare the amounts etc. But I was vary of publishing the app. Due to various reasons. But nor did I want my hard word to rot. Silicon chips may not rot, but any ways. So I would write few lines of code, take all screen shots, check it in my device. Then sit idle for many many days. Literally idle.  Today, I had an idea. Let me donate it. The source code. So that some MCA student can use it for his project. Or some one else may use it as a hobby app. Some thing.  Any ways, now it is in github . Download the source code of the app and see if it helps you in any way. So basically the app does the fol...

Password recovery in Linux Mint

 If you are like me, you have millions of passwords and you tend to change them frequently. But what happens if you can't login to your Linux machine? Shift key :  Often, your mint system does not show you grub screen to select the boot option. But you need that for recovery option. So, while boot, continuously click on shift key.  This will bring up the advanced options. Edit this menu option and change ro to rw, because you want your system to be read/write. Now boot the recovery option using F10. And descend to boot as root. Now you will see boot prompt - #.  Give passwd command as  passwd username   and change your password.  Now I can say proudly that along with installing OS, I can also recover password in Linux system. 

Forcing password changes in Linux - chage

 If you are like me - ultra paranoid, you are not happy with one password. You want to change the password frequently (before the hacker some how solves it ;) ). chage is the command for you. chage - there is no n here - determines after how many days the password of a user expires.  This is what man page says about it          The chage command changes the number of days between password changes and the date of the last password change. This information is used by  the system to determine when a user must change his/her password. You can set number of days after which password expires, when do you want to be prompted for it etc.  $sudo chage username [sudo] password for username: Changing the aging information for username Enter the new value, or press ENTER for the default     Minimum Password Age [0]:     Maximum Password Age [10]:     Last Password Change (YYYY-MM-DD) [2021-08-26]: ...

Shataka - a game to get century

 Let us see if you can solve this puzzle. You remember the rules! You have to get 100 as sum of every row and sum of every column.  If you feel you can solve it in no time, why don't you download the app, and solve other levels too.

Shataka

 The number game Shataka is published. The game is to rearrange 9 numbers in a grid to get a sum of 100 in rows and in columns. I think you will like it. Install it now a nd see for yourself

Coming soon - a math game

 Some times I surprise myself. May be the sentence must have been in past tense. As of now, the surprises more like shocks. But some times I do get some good ideas. Like this game shataka .( I had changed the name to hundred. Now I need to think of a catchier name) People tend to confuse it with Sudoku. But unlike sudoku, shataka is a small game. Instead of 8 1 cells in the grid, there are only 9 cells.  And unlike sudoku, this game involves calculations - addition and lot of it. You don't just pick a number and place in a cell. You place the number if it makes the sum of row as 100 and sum of column as 100. Easier said than done.  So now where is this brain-child of mine which I am so fond of? It is under "construction". Which means I am applying coats of makeup, then washing it off again. Try different shade etc. That is to say, I am fiddling with UI - my not such a good friend.  The look so far.

source code of simple memory game - Simon

Simon game is an electric memory game.  The device creates a series of tones and lights and requires a user to repeat the sequence. If the user succeeds, the series becomes progressively longer and more complex. Once the user fails or the time limit runs out, the game is over. Read in wikipedia. This android game is a simulation of that.  The buttons on the screen glow in some order with their notes. The user must reproduce these button clicks in the same order.  It is a very good game for memory with the added benefit of audio feature.  The source code of the game is available in my github page  Simon  The android game with Simon and 3 more memory games by Hegdeapps can be downloaded from google play  Basically in our layout file, we have four SimonCell custom widgets which are extended classes of image views. These are the four colored buttons you see in the image above.              ...

A maths puzzle in Javascript

 I have written an app called Hundred.  The app has a grid of 3 by 3. Some numbers in the grid are given. You have to fill the rest of the numbers with the condition that sum of each row and sum of each column is 100.  I have written the same using HTML and java script now.  Here is the source code of the same. HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8" />   <meta name="viewport" content="width=device-width, initial-scale=1">   <style> .grid-container {   width:auto;   display: grid;   grid-row-gap:2px;   grid-column-gap:2px;   grid-template-columns: 80px 80px 80px ;   padding: 10px; } .grid-item {   background-color: rgba(255, 255, 255, 0.8);   border: 1px solid rgba(0, 0, 0, 0.8);   padding: 20px;   font-size: 22px;   text-align: center; } </style> <script type="text/javascript" src="shataka.js"></script> </head> <body> ...

Count the number of leaf nodes

 Qn: Write a function to count the number of leaf nodes in a binary search tree. Solution:  count = leaf nodes in left subtree + leaf nodes in right subtree If a node is having left link as NULL and right link as NULL, then it is a leaf node. So if this condition is true, the call should return 1. If the node is NULL, the call should return 0. if node is NULL return 0 if node->left==NULL and node->right==NULL return 1 else return count(node->left)+count(node->right)

Count the number of nodes of a binary tree

 Question: How do you count the nodes of a binary tree? Solution :  Like almost all functions in a binary tree, this algorithm also needs a recursive function.  The no. of nodes of a binary tree = 0 if the node is NULL = 1 +  no. of nodes in left sub-tree+ no.of nodes in right sub-tree. Calculate recursively for all sub-trees. Here is C++ code for the same.  int bintree::count() { return count_leaf_nodes(root); } int bintree::count_nodes(node * nd) { if(nd==NULL) return 0; if(nd->left ==NULL && nd->right==NULL) return 1; return 1+count_nodes(nd->left)+count_nodes(nd->right); }  

Deleting a node from binary tree

Write a program to delete a node from binary search tree. Solution : The problem with binary tree operations is that it is nonlinear.  When we delete a node from linked list, we link the previous node to the next node so that the list is not broken. But that is not simple in binary tree. Because the node has two next nodes - left child node and right child node. Which do we link to previous node - or parent node? If we link say left child node to the parent of node to be deleted, what do we do with other branch? When a node is to be deleted from a binary search tree, there are 3 possible cases. the node is leaf the node has only one child/subtree the node has both children/subtrees The following recursive algorithm takes care of all three cases nd = root find the node to be deleted  if nd->val > delvalue recursively call delete on nd->left and set result to nd->left if nd->val < delvalue recursive, call delete on nd->right and set result to nd->right no...

Reverse a singly linked list

 I think I have already blogged it some where. It is very common question - next only to inversing the binary tree.  Write a function to reverse a singly linked list. Would you like to write a recursive function? No, OK, let us write a non-recursive function for this. Let us say you have first three nodes n1, n2 and n3. Now simply link n2 to n1. Next take next three nodes n2,n3 and n4 and link n3 to n2. Continue this process until you reach the end of the list. Take n1, n2 = n1->next and n3 = n2->next Set n1->next = NULL. Because this will be your last node.  Now link n2 and n1. That is set n2->next = n1. Now assign n1 = n2,  n2 = n3 and n3 = n3->next Repeat 2 steps  above until n3 = NULL. You still have one un-assigned node. Set that. n2->next = n1 Now n2 is your new head node.  That's it.  And now it is the coding time. #include<iostream> using namespace std; struct node { int n; node *next; }; class...

Concatenation of two circular linked lists

 Here is a trick question - a data structures question for you Write a function to concat two circular linked lists. Without traversing either of them. The lists are singly linked. hint : order is not important. Let us try to understand the situation. We have two single linked circular lists. We have to join them.  One easy solution would be to traverse to the end of first list and link the last node to second list. But we are not allowed to do that.  The second possible solution could be find the previous node of head of first list - which will be last node. And then link it to second list. Again, not possible as the list is not doubly linked. Ok. Here is the solution  Let temp be the next node of head of list1 Link head of list1 to head->next of list2 Link head of list2 to temp Let us take an example. Let the two lists be  1-2-3-4-5 and 10-20-30 Now let temp=2 Assign  1->next =20.  Now we have list1 as 1-20-30-10. and back to 20 Assign 10->n...

Program to add a node to rear of circular linked list

 Let us write Write a program to add a node to the rear of a circular linked list. Now a circular linked list has no end. That is to say, the last node in the list points back to the head of the list. So finding the last node will be slightly tricky. let temp = head while temp->next !=head temp = temp->next Once we find the last node, we append the new node here. But we must not forget to point his new node back to head. lastnode->next = newnode; newnode->next = head; The complexity is O(n).   Here is the complete program #include<iostream> using namespace std; struct node { int n; node *next; }; class linked_list { node *head; public: linked_list(int val = 0) { if(val==0) head = NULL; else{ head = new node; head->n = val; head->next = NULL; } } node* find_last_node() { if(head==NULL || head->next==head) ...

Three questions and one program

I must be missing coding. So let me write few lines of code and you tell me the output. 1) int a = 10; if(a&1) cout<<"Hello"; else cout<<"hi"; 2) for(int i=2;i<20;i++) if(i&(i-1)==0) cout<<i<<" "; 3) int i =10; cout<<(i<<3)<<" "; cout<<(i>>3)<<" "; Please do not open another window and go to online compilers.  And as I struggled a bit for this, let us see how fast you can come up with the solution.   Write a program to read two fractions and find their sum.  No, do not give me the answer in decimal. The answer should be in the form of fraction. 
 Do you want to take a quiz on C++ Classes and objects? You can take an online quiz in my page https://ushahegde.github.io/mywebpage/CppQns/Class.html

Silly sentence

Do you want to display silly sentences? And using code? Have three arrays with subjects, objects and verbs and randomize them.  I have used latin words in the program, you can replace them with any language of your choice and amuse your friends. #include<iostream> using namespace std; class sentence { string subject; string object; string verb; public: sentence(); sentence(string,string,string); void print_sentence(); }; sentence::sentence() { subject = ""; object = ""; verb = ""; } sentence::sentence(string sub,string obj,string verb) { this->subject = sub; this->object = obj; this->verb = verb; } void sentence::print_sentence() { cout<<subject<<" "<<object<<" "<<verb<<endl; } int main() { string sub_arr[]= {"agricola","regina","filia",...

Rinda rinda - Java Quizard

 Rinda rinda govinda - that's what my dad says for some words which are told again and again and again. But isn't that marketing is all about? So let me talk of my android app called Java Quizard - Java Quiz + Java wizard. Yes, supposedly it will make you a wizard in Java. Will it? Really?  I selected this name as the continuation of my earlier app C quizard - An app with plenty of C quiz to make you a C wizard.  This Java app has good many questions on Java basics. It included topics like basics, classes, inheritance, exception handling and multi threading.  So if you feel you need a free app to help you Java language revision - not Java coffee, and you can have a look at my app.