Created
August 19, 2018 17:45
-
-
Save preetham-salehundam/c14fce20c11854c60abf7de28171a81e to your computer and use it in GitHub Desktop.
Pre-Reqs-Fundamentals-Of-CS-Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package demo.tutorials.demo; | |
import java.util.List; | |
/** | |
* Created by Preetham on 04/08/18. | |
* | |
* pseudo code | |
* | |
* start from head of the linkedlist 1 | |
* assign next node's ref to head | |
* assign list 2 to tail node of linkedlist1 when next node ref is null | |
* and repeat till node is not null | |
* | |
*/ | |
class ListNode { | |
int dataVal; | |
ListNode next; | |
public ListNode() { | |
} | |
public ListNode(int data, ListNode next) { | |
this.dataVal = data; | |
this.next = next; | |
} | |
public void appendNode(int data, ListNode next) { | |
this.dataVal = data; | |
this.next = next; | |
} | |
public void print() { | |
/*ListNode currentNode = this; | |
while (currentNode != null) { | |
System.out.println(currentNode.dataVal); | |
currentNode = currentNode.next; | |
}*/ | |
// short hand way | |
for(ListNode currentNode = this; currentNode!=null; currentNode = currentNode.next){ | |
System.out.println(currentNode.dataVal); | |
} | |
} | |
} | |
public class AppendLL { | |
public static ListNode appendLists(ListNode list1, ListNode list2) { | |
// ListNode currNode = list1; | |
// while (currNode != null) { | |
// | |
// if (currNode.next == null) { | |
// currNode.next = list2; | |
// break; | |
// } | |
// currNode = currNode.next; | |
// | |
// } | |
// smarter version | |
for(ListNode currentNode = list1; currentNode != null ; currentNode = currentNode.next){ | |
if(currentNode.next==null){ | |
currentNode.next = list2; | |
break; | |
} | |
} | |
return list1; | |
} | |
; | |
public static void main(String[] args) { | |
ListNode ll1 = new ListNode(); | |
ll1.appendNode(1, new ListNode(2, new ListNode(3, null))); | |
System.out.println("******* print list 1"); | |
ll1.print(); | |
ListNode ll2 = new ListNode(10, new ListNode(11, new ListNode(12, new ListNode(13, null)))); | |
System.out.println("******* print list 2"); | |
ll2.print(); | |
ListNode ll3 = appendLists(ll1, ll2); | |
System.out.println("******* print appended list "); | |
ll3.print(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package demo.tutorials.demo; | |
import java.util.Scanner; | |
/** | |
* Created by Preetham on 19/08/18. | |
*/ | |
public class Diamond { | |
private final static String SPACE = " "; | |
private final static String STAR = "*"; | |
public static void main(String[] args) { | |
Scanner scan = new Scanner(System.in); | |
System.out.print("Please enter the number of rows to be printed: "); | |
int rows = scan.nextInt(); | |
for (int row = 1; row <= rows; row++) { | |
int column = row; | |
int spaces = rows - column; | |
int stars = column; | |
// prints one half of pyramid offset with spaces | |
// * | |
// ** | |
// *** | |
for (int space = 1; space <= spaces; space++) { | |
System.out.print(SPACE); | |
} | |
for (int star = 1; star <= stars; star++) { | |
System.out.print(STAR); | |
} | |
// prints the other half starting from second row , first row should contain a single star | |
/* | |
* | |
** | |
*** | |
**** | |
*/ | |
for (int star = 2; star <= stars; star++) { | |
System.out.print(STAR); | |
} | |
// * | |
// *** | |
// ***** | |
// ******* | |
// next line | |
System.out.println(); | |
} | |
// reverse the above process | |
for (int row = rows; row >= 0; row--) { | |
int column = row; | |
int spaces = rows - column; | |
int stars = column; | |
for (int space = spaces; space > 0; space--) { | |
System.out.print(SPACE); | |
} | |
for (int star = stars; star > 0; star--) { | |
System.out.print(STAR); | |
} | |
// should be > or equal to 0 here. | |
for (int star = stars - 2; star >= 0; star--) { | |
System.out.print(STAR); | |
} | |
System.out.println(); | |
} | |
//} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package demo.tutorials.demo; | |
import java.util.Scanner; | |
/** | |
* Created by Preetham on 03/08/18. | |
*/ | |
public class PrintNthItemArr { | |
public static void printEveryNthItem(int N, int[] arr){ | |
for (int index = 0; index < arr.length; index++) { | |
// check for multiples | |
if (index % N == 0) { | |
if (N < arr.length) { | |
System.out.print(arr[index]); | |
} else { | |
throw new ArrayIndexOutOfBoundsException(); | |
} | |
} | |
} | |
}; | |
public static void main(String[] args) { | |
Scanner scan = new Scanner(System.in); | |
System.out.println("Enter the N"); | |
int N = scan.nextInt(); | |
int[] arr = new int[]{3, 4, 7, 2, 1, 4, 8, 4, 9, 0, 1, 6}; | |
printEveryNthItem(N, arr); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package demo.tutorials.demo; | |
/** | |
* Created by Preetham on 31/07/18. | |
* Program to print pyramid of stars | |
* pseudo code: | |
* | |
* when rows=5, | |
* for row=1 and row less than rows | |
* column= row | |
* Print second half of pyramid : | |
* number of spaces = rows-column | |
* for 'number of spaces' times print a space | |
* for number of ‘column’ times print star | |
* Print first half of pyramid stacking after the second half: | |
* for number of ‘column’ times print a star again , but starting from second row | |
* row = row+1 | |
* print \n | |
*/ | |
import java.util.Scanner; | |
public class UpperTriangle { | |
private final static String SPACE = " "; | |
private final static String STAR = "*"; | |
public static void main(String[] args) { | |
Scanner scan = new Scanner(System.in); | |
System.out.print("Please enter the number of rows to be printed: "); | |
int rows = scan.nextInt(); | |
for (int row = 1; row <= rows; row++) { | |
int column = row; | |
int spaces = rows - column; | |
int stars = column; | |
// prints one half of pyramid offset with spaces | |
// * | |
// ** | |
// *** | |
for (int space = 1; space <= spaces; space++) { | |
System.out.print(SPACE); | |
} | |
for (int star = 1; star <= stars; star++) { | |
System.out.print(STAR); | |
} | |
// prints the other half starting from second row , first row should contain a single star | |
/* | |
* | |
** | |
*** | |
**** | |
*/ | |
for (int star = 2; star <= stars; star++) { | |
System.out.print(STAR); | |
} | |
// * | |
// *** | |
// ***** | |
// ******* | |
// next line | |
System.out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment