Posts

Remove k digits

class Solution: def removeKdigits(self, num: str, k: int) -> str:     if len(num) == k:#no need to search, just return 0         return str(0)          point = 0 #use the point to traverse the num     while(k>0 and point < len(num)-1):         if num[point] > num[point+1]: #from left to right, each time we check if the digit that is larger than the digit behind             num = num[:point]+num[point+1:] #if so, delete that digit             k-=1             if point!=0:#except point equals to 0, we step back one digit                 point-=1             continue #jump the point+=1 statement         point+=1      if k == 0:#num[:-k] is not valid if k==0         return  str(i...

Dicegame

< html >      < head >      < script   type = "text/JavaScript" >       var   total1 = 0 ;       var   total2 = 0 ;       function   rollDice1 ()      {          var   die1 , die2 , d1 , d2 ;          die1 = document . getElementById ( "die1" );       die2 = document . getElementById ( "die2" );       status1 = document . getElementById ( "status1" );       scoreof1  =  document . getElementById ( "scoreof2" );       d1 = Math . floor ( Math . random ()*  6  ) +  1 ;       d2 = Math . floor ( Math . random ()*  6  ) +  1 ;      ...

Sum of Non Lower Triangle Elements:

Sum of  Non Lower Triangle Elements: Program: import java.util.Scanner; public class NonLowerTriangleElementsSum { public static void main(String[] args) { int r, c; Scanner s = new Scanner(System.in); r = s.nextInt(); c = s.nextInt(); int[][] matrix = new int[r][c]; for(int i = 0; i<r; i++) { for(int j = 0; j<c;j++) { matrix[i][j] = s.nextInt(); } } int sum = 0; for(int i = 0; i<r; i++) { for(int j = 0;j<c; j++) { if(i<j) { System.out.print(matrix[i][j]); System.out.print(" "); sum+=matrix[i][j]; } } } System.out.println("\nThe sum of Non Lower triangular elements of a matrix is: " + sum); } } Output: 3 3  1 2 3 4 5 6 7 8 9 2 3 6  The sum of Non Lower triangular elements of a matrix is: 11 Happy Coding💻:-)

Sum of Each Row of A Matrix

Sum of Each Row of A Matrix: Program: import java.util.*; public class MatrixRowSum { public static void main(String[] args) { int r, c; Scanner s = new Scanner(System.in); r = s.nextInt(); c = s.nextInt(); int[][] matrix = new int[r][c]; for(int i = 0; i<r; i++) { for(int j = 0; j<c;j++) { matrix[i][j] = s.nextInt(); } } int sum = 0; for(int i = 0; i<r; i++) { sum = 0; for(int j = 0;j<c; j++) { sum += matrix[i][j]; } System.out.println(i+1 + "  " + "Row sum is: " + ""+ sum); } } } Output: 1) 3 3 1 2 3 4 5 6 7 8 9 1  Row sum is: 6 2  Row sum is: 15 3  Row sum is: 24 2) 3 4 1 2 3 4 5 6 7 8 9 10 11 12 1  Row sum is: 10 2  Row sum is: 26 3  Row sum is: 42                                                                 ...

Rotate String in Right Direction in java

Rotate String in Right Direction in java: Program: import java.util.*; public class RotateString { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); char[] ch = new char[n]; for(int i=0;i<n;i++) { ch[i] = s.next().charAt(0); } int r = s.nextInt(); char temp = 'a'; r=r%n; while(r>0) { temp = ch[n-1]; for(int i = n-1; i>0;i--) { ch[i] = ch[i-1]; } ch[0] = temp; System.out.println("r: " + r); for(int i=0;i<n;i++) { System.out.print(ch[i]); } System.out.println(); r--; } for(int i=0;i<n;i++) { System.out.print(ch[i]); } } } Output : 5 a s d f g 3 r: 3 gasdf r: 2 fgasd r: 1 dfgas dfgas                                                            Happy Coding 💻:-)

Sum of Each Column of a Matrix

Sum of Each Column of a Matrix: Program: import java.util.Scanner; public class MatrixColumnSum { public static void main(String[] args) { int r, c; Scanner s = new Scanner(System.in); r = s.nextInt(); c = s.nextInt(); int[][] matrix = new int[r][c]; for(int i = 0; i<r; i++) { for(int j = 0; j<c;j++) { matrix[i][j] = s.nextInt(); } } int sum = 0; for(int j = 0; j<c; j++) { sum = 0; for(int i = 0;i<r; i++) { sum += matrix[i][j]; } System.out.println(j+1 + "  " + "Column sum is: " + ""+ sum); } } } Output : 3 3 1 2 3 4 5 6 7 8 9 1  Column sum is: 12 2  Column sum is: 15 3  Column sum is: 18                                                           Happy Coding 💻:-)

Sum of Elements of Lower Triangular Matrix

Sum of Elements of Lower Triangular Matrix: Program: import java.util.Scanner; public class LowerTriangularElementsSum { public static void main(String[] args) { int r, c; Scanner s = new Scanner(System.in); r = s.nextInt(); c = s.nextInt(); if(r!=c) { System.exit(0); } int[][] matrix = new int[r][c]; for(int i = 0; i<r; i++) { for(int j = 0; j<c;j++) { matrix[i][j] = s.nextInt(); } } int sum = 0; for(int i = 0; i<r; i++) { for(int j = 0;j<c; j++) { if(i>=j) { System.out.print(matrix[i][j]); System.out.print(" "); sum+=matrix[i][j]; } } } System.out.println("\nThe sum of Lower triangular elements of a matrix is: " + sum); } } /* In other words, a square matrix is lower triangular if all its entries above the main diagonal are zero.  * Example of a 3 × 3 lower triangular matrix: Diagonal matrices are both upper  *and lower triangular since they have zeroes a...