printing result
1 print("The mirror string : " + str(res))
2
3 // output:
4
5 /usr/bin/python3.10 /home/sanjib/Documents/development/test-python/hello.py
6
7 The original string is : sanjib
8 The mirror string : bijnas
==========
In Second case Language used: Java ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1 // code
2
3 package fun.sanjibsinha;
4 /*
5 Some more Array methods using external apache commons lang3
6 */
7
8 import org.apache.commons.lang3.ArrayUtils;
9
10 public class A18 {
11
12 public static void main(String[] args){
13 //we can reverse an Array
14 char[] myName = {'s', 'a', 'n', 'j', 'i', 'b'};
15 //now we can just add this characters to get my name
16 System.out.println("My name : " + new String(myName));
17 //let us reverse thsi character to see how my name looks in the mirror
18 ArrayUtils.reverse(myName);
19 System.out.println("My name on the mirror : " + new String(myName));
20 }
21 }
22
23 OUTPUT
24 ------
25
26 My name : sanjib
27 My name on the mirror : bijnas
’’’’’’’’’ In the second case, with the help of these external libraries we can reverse any array components in less lines of code. In usual case, we need to write extra code to get the same result.
However, in Dart Programming langauge, it takes only one line of code.
’’’’’’’’’
In Third case Language used: Dart ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1 main() {
2
3 var input = "sanjib";
4 print(String.fromCharCodes(input.runes.toList().reversed));
5
6 }
7
8 ---output---
9
10 bijnas
Challenge 3 : According to the Set theory, the Union occurs between two Sets and the output omits the common value. Can you write a program to prove this concept.
Solution for Challenge 3:
Language used: PHP 8
1 // code
2
3
4 <?php
5
6 /*
7 * They have similarities and differences
8 * Set theory of Discrete mathematics and PHP has many similarities
9 * Both represent data sets
10 Both hold a list of similar elements
11 We can operate on both by performing union, intersection etc
12 */
13
14 // consider two separate arrays
15
16 $arrayOne = [11, 12, 13];
17 $arrayTwo = [11, 12, 13, 14, 15];
18
19 // consider one universal array that contain elements of both arrays
20
21 $universalArray = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
22
23 /*
24 * According to the set theory, the union occurs as
25 * $arrayOne + $arrayTwo
26 * the output omits the common value
27 * we can do the same using PHP
28 */
29
30 $unionOfTwoArrays = $arrayOne + $arrayTwo;
31
32 echo "Union of two arrays : [" . implode(", ", $unionOfTwoArrays) . "]<br>";
33
34 /*
35 * We can do the difference operation using the same technique
36 * we can use the PHP default methods
37 */
38
39 $differenceOfTwoArrays = array_diff($arrayTwo, $arrayOne);
40
41 echo "Difference of two arrays : [" . implode(", ", $differenceOfTwoArrays) . "]<br>\
42 ";
43
44
45 /*
46 * We can do the intersection operation using the same technique
47 * it keeps the same elements
48 * we can use the PHP default methods
49 */
50
51 $intersectionOfTwoArrays = array_intersect($arrayOne, $arrayTwo);
52
53 echo "Intersection of two arrays : [" . implode(", ", $intersectionOfTwoArrays) . "]\
54 <br>";
55
56 /*
57 * We can do the complement operation on two arrays
58 * we can use the PHP default methods
59 */
60
61 $arrayOneComplement = array_diff($universalArray, $arrayOne);
62
63 echo "Complement of arrayOne : [" . implode(", ", $arrayOneComplement) . "]<br>";
64
65 $arrayTwoComplement = array_diff($universalArray, $arrayTwo);
66
67 echo "Complement of arrayTwo : [" . implode(", ", $arrayTwoComplement) . "]<br>";
68
69 // output
70
71 Union of two arrays : [11, 12, 13, 14, 15]
72 Difference of two arrays : [14, 15]
73 Intersection of two arrays : [11, 12, 13]
74 Complement of arrayOne : [14, 15, 16, 17, 18, 19, 20]
75 Complement of arrayTwo : [16, 17, 18, 19, 20]
Challenge 4 : Write a program where every element of the array is the same. Find out the probability.
Solution for Challenge 4:
Language used: Python 3.10
1 // code
2
3 # we are to find the probability of finding an element in an array
4 # it depends on two factors
5
6 # Probability = number of favorable outcome / number of possible outcomes
7 # number of possible outcomes is the length of the array
8 # number of favourable outcome is the total number of the element in the list
9 # Probability = total number of the element present / size or length of the array.
10
11 # define the function to find the probability
12 def findTheProbability(theArray, theLenghtOfArray, theElement):
13 count = theArray.count(theElement)
14
15 # find probability up to 4 decimal places
16 return round(count / theLenghtOfArray, 4)
17
18 theArrayVariable = [22, 22, 22, 22, 22, 22, 22]
19 theElementToFind = 22
20 theLenghtOfTheArrayVariable = len(theArrayVariable)
21
22 print(findTheProbability(theArrayVariable, theLenghtOfTheArrayVariable, theElementTo\
23 Find))
24
25 // output
26 // Since every element of the array is the same, the probability is 100 percent.
27
28 1.0