Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts
mixString  function in Java (CodingBat Solution)

mixString function in Java (CodingBat Solution)

Problem :

Given two strings, a and b, create a bigger string made of the first char of a, the first char of b, the second char of a, the second char of b, and so on. Any leftover chars go at the end of the result.

mixString("abc", "xyz") → "axbycz"
mixString("Hi", "There") → "HTihere"
mixString("xxxx", "There") → "xTxhxexre"

Solution :

package com.nextgen4it.problems;

public class mixStringfunction {
                public String mixString(String a, String b) {
                                String result = "";

                                for (int count = 0; count < Math.max(a.length(), b.length()); count++) {
                                                if (count < a.length())
                                                                result += a.substring(count, count + 1);
                                                if (count < b.length())
                                                                result += b.substring(count, count + 1);
                                }
                                return result;
                }
                public static void main(String[] args) {
                                mixStringfunction m=new mixStringfunction();
                                System.out.println(m.mixString("abc", "xyz"));
                }
}


Ouput :


axbycz
xyzMiddle function in Java (CodingBat Solution)

xyzMiddle function in Java (CodingBat Solution)

Problem:

Given a string, does "xyz" appear in the middle of the string? To define middle, we'll say that the number of chars to the left and right of the "xyz" must differ by at most one. This problem is harder than it looks.

xyzMiddle("AAxyzBB") → true
xyzMiddle("AxyzBB") → true
xyzMiddle("AxyzBBB") → false


Solution :
package com.nextgen4it.problems;

public class xyzMiddlefunction {

                public boolean xyzMiddle(String str) {

                                String xyz = "xyz";

                                int len = str.length();

                                int middle = len / 2;

                                if (len < 3)

                                                return false;

                                if (len % 2 != 0) {

                                                if (xyz.equals(str.substring(middle - 1, middle + 2))) {

                                                                return true;

                                                } else {

                                                                return false;

                                                }

                                } else if (xyz.equals(str.substring(middle - 1, middle + 2)) ||

                                                                xyz.equals(str.substring(middle - 2, middle + 1))) {

                                                return true;

                                } else

                                                return false;

                }
public static void main(String[] args) {
                xyzMiddlefunction x=new xyzMiddlefunction();
                System.out.println(x.xyzMiddle("AxyzBBB"));
}
}


Output :

false


repeatEnd function in Java (CodingBat Solution)

repeatEnd function in Java (CodingBat Solution)

Problem 2:
Given a string and an int n, return a string made of n repetitions of the last n characters of the string. You may assume that n is between 0 and the length of the string, inclusive.

repeatEnd("Hello", 3) → "llollollo"
repeatEnd("Hello", 2) → "lolo"
repeatEnd("Hello", 1) → "o"

Solution:
package com.nextgen4it.problems;
public class repeatEndfunction {
                public String repeatEnd(String str, int n)

                {
                                String res = str.substring(str.length() - n);
                                for (int i = 1; i < n; i++)
                                                res = res + str.substring(str.length() - n);
                                return res;
                }
                public static void main(String[] args) {
                                repeatEndfunction r=new repeatEndfunction();
                                System.out.println(r.repeatEnd("Hello", 3));
                }
}

Output :

llollollo


 starOut function in Java (CodingBat Solution)

starOut function in Java (CodingBat Solution)

Problem:

Return a version of the given string, where for every star (*) in the string the star and the chars immediately to its left and right are gone. So "ab*cd" yields "ad" and "ab**cd" also yields "ad".

starOut("ab*cd") → "ad"
starOut("ab**cd") → "ad"
starOut("sm*eilly") → "silly"

Solution:

package com.nextgen4it.problems;

class starOutfunction {

                public String starOut(String str) {
                                String result = "";
                                for (int i = 0; i < str.length(); i++) {
                                                if (str.charAt(i) == '*') {

                                                } else if (i != 0 && str.charAt(i - 1) == '*') {
                                                } else if (i != str.length() - 1 && str.charAt(i + 1) == '*') {
                                                } else {
                                                                result += str.charAt(i);
                                                }
                                }
                                return result;
                }

                public static void main(String[] args) {
                                starOutfunction s = new starOutfunction();
                                System.out.println(s.starOut("sm*eilly"));
                }
}

Output:

silly

Let's get started with some simple numerical computations in Bash

Let's get started with some simple numerical computations in Bash

As can be observed from the examples below, there are several ways of making simple numerical calculations in Bash. Just trying to echo an expression wrapped in quotation marks will not work. Wrapping the expression in double parenthesis $((..)) evaluates it, but this is confined to integer computations. To evaluate expressions involving decimal places (floating points) "bc -l" is very useful.

~$ echo "5+5"
5+5
~$ echo "5+5"| bc
10
~$ echo "5+5"| bc -l
10
~$ echo "5+5.2"| bc -l
10.2
~$ echo "5+5.2"| bc
10.2
~$ echo "3/4"| bc
0
~$ echo "3/4"| bc -l
.75000000000000000000
~$ echo $((3+3))
6 

To display the final result by rounding it to a certain number of decimal places, "printf" with a format specified can accomplish the task by specifying the "scale" (number of decimal points). Note that the ordering of the numbers matters in this case, as demonstrated below.

~$ echo "scale = 2; 10 * 100 / 30" | bc
33.33
~$ echo "scale = 2; 10 / 30 * 100" | bc
33.00
~$ echo "scale = 2; (10 / 30) * 100" | bc
33.00
'Expr' is another way to accomplish such tasks.
~$ echo $(expr 5 + 5)
10
~$ echo $(expr 5 - 5 + 2 )
2
~$ echo $(expr 5 - 5 + 2 / 3 )
0
~$ echo $(expr 5 - 5 + 2 / 1 )
2

Be careful with spacing in such expressions! Bash is very sensitive to them.