Write the method that takes four integer arguments and returns true if and only if at least three of the four values are the same - boolean

Write the method public static boolean atLeastThreeMatch(int a,
int b, int c, int d) that takes four integer arguments and returns true if and only if at least three of the four values are the same. For example, if the values passed in are 5, 3, 5, 5, it would return
true. If the values passed in are 7, 3, 7, 9, it would return false.
To complete the task I wrote every possible condition that would satisfy the desired output. I didn't like doing it that way and I feel there is an easier way to acquire the desired output. Any thoughts?

cehck below code snippet in which Set is preserving all input values. Set contains only distinct values so if set contains less/equal to 2 values that means atleast three values are equal.
public boolean atLeastThreeMatch(int a, int b, int c, int d) {
List<Integer> values = Arrays.asList(a, b, c, d);
Set<Integer> counter = new HashSet<Integer>();
counter.addAll(values);
if(counter.size() <=2){
return true;
} else {
return false;
}
}

Try to create a counter that counts the equalities between numbers. For example:
int counter=0;
if(num1 == num2)
counter++;
At last:
return counter==3;

Related

Flutter int value if first digit is not zero I want to get first value

Flutter int value if first digit is not zero I want to get first value
for example
int x=123
result =1;
another example
int y=234;
result=2;
if the first value is zero I want to get the second value
for example
int a=023;
result=2;
another example
int b=098;
result=9;
how can i do this using dart?
There surely is a more mathematical way, but one way to do this is to convert the int to a String, get the first character and parse it back to int :
var numbers = [
1234,
567,
89,
];
for(var number in numbers) {
var firstNumber = int.parse(number.toString()[0]);
print(firstNumber);
}
Output :
1
5
8
This will give you the first non-zero digit. Assuming you get your number as a String. Otherwise it is pointless because the int value = 0123 is the same as int value = 123.
String yourNumber = "0123";
final firstNonZero = int.tryParse(yourNumber.split('').firstWhere((element) => element != '0', orElse: () => ''));
print(firstNonZero);

Swift Index and it's implementation

I'm new to programming. I was trying to understand how indices work in swift. This is the following code from swift documents.
converted into function.
func ind(){
var c = [10, 20, 30, 40, 50] //index = [0:5]
var i = c.startIndex //i = [0]
while i != c.endIndex { // i!= [5] , while i not equal to [5]
c[i] /= 5
i = c.index(after: i)
}
print(c) // [2,4,6,8,10]
}
the code line i = c.index(after: i) doesn't seems to make sense to me. "after" means the character of string after the string.index, but because we initialized the 'i' to be zero(0) the output should stay [4 and onwards]. secondly, if i replace the i let's say with integer 2. the loop keeps repeating itself. why? Thank you in advance for your time
after means the next element on your list in this context.
An index is more general and is not limited to String.
In addition, an index can have different types depending on the structure that you are manipulating.
For instance:
var c1 = [10, 20, 30, 40, 50] //
var i1 = c1.startIndex
// Print: Int
print(type(of: i1))
var c2 = "Hello" //
var i2 = c2.startIndex
// Print: Index
print(type(of: i2))
You can even create your own index for a specific type, as long as you provide a way to compute the next index.
Thus, in your code for this particular example, the index is of type Int.
If you change the value of i to be the constant 2, you can never equal the value of the end index (which is 5 here).
Then, your loop will never end.

Generating two different random number simultaneously in Scala - Spark

Can anybody help me generate two different random numbers in two ranges? I've tried:
var a = Random.nextInt(S)
var b = Random.nextInt(K)
if (a == S || b == K){
a = S-1
b = K-1
}
(word,a,b)
But this generates some numbers that are not in the specified ranges.
From the docs on Random:
def nextInt(n: Int): Int
Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.
By the method contract, nextInt will always return a value from 0 to n - 1, so your condition a == S || b == K will always be false.

Looking for Specman methods to get first/next/last enumerated value

Are there built-in methods in Specman that give me the value of the first, last, and next value of an enumerated variable?
For example:
type color_e: [blue, green, red, yellow, orange];`
var color: color_e;
color = <want to assign first value of color_e>; //blue
…
color = <next value>; //which is green
you can use the all_values() method, which returns a list of all value of the enumerated type. You can then play with the list as you want.
example:
type color : [RED, BLUE, GREEN];
extend color : [BLACK, CYAN];
extend sys {
run() is also {
var x : color;
foreach in all_values(color) {
x = it;
print x;
};
};
};
Running the test ...
x = RED
x = BLUE
x = GREEN
x = BLACK
x = CYAN
I got the following reflection solution, but woner if there is an easier way.
<'
----------------------------------------------------------------------
-- Define an enumerated type and populate
----------------------------------------------------------------------
type color_e : [blue, green, red, yellow, orange];
----------------------------------------------------------------------
-- Extend sys
----------------------------------------------------------------------
extend sys {
----------------------------------------------------------------------
-- Use the reflection facility to get the number of labels in an
-- enumerated type
----------------------------------------------------------------------
get_num_labels(enum_item: string) : int is {
var size : int; -- number of labels defined in the enumerated type
-- Find the type if defined
for each (t) in rf_manager.get_user_types() {
if t is a rf_enum and t.get_name() == enum_item {
-- return the size of the list
result = t.as_a(rf_enum).get_items().size();
}; // if t is a rf_en...
}; // for each (t) in...
}; // get_num_labels( ...
----------------------------------------------------------------------
-- Use the reflection facility to return the value of the
-- label defined in an enumerated type at a particular index
----------------------------------------------------------------------
get_enum_label_value(enum_item: string, idx: int) : int is {
var size : int; -- number of labels defined in the enumerated type
-- Find the type if defined
for each (t) in rf_manager.get_user_types() {
if t is a rf_enum and t.get_name() == enum_item {
-- get the size of the list
size = get_num_labels(enum_item);
-- return the value of the item in the list at the specified index if legal
if (idx < size) {
result = t.as_a(rf_enum).get_items()[idx].get_value();
} else {
dut_errorf("Index requested is greater than the number of labels in the enumerated type %s\n",enum_item);
};
}; // if t is a rf_en...
}; // for each (t) in...
}; // get_enum_label_value( ...
----------------------------------------------------------------------
-- Extend the run phase
----------------------------------------------------------------------
run() is also {
var color : color_e;
var size : int;
size = get_num_labels("color_e");
messagef (NONE, "Enumerated type color_e has %d labels, and they are...\n", size);
for i from 0 to (size-1) {
color = get_enum_label_value("color_e",i).as_a(color_e);
messagef (NONE, "%s\n", color);
}; -- for
}; // run is also
}; // extend sys
'>
Another possible solution (in addition to all_values()) is with reflection. Given a rf_enum, we can call its get_items() method to get a list of all enum items.
However, there are some differences between the refleciton soluition and all_values(), which are important to know.
One difference is that if some enum items are given explicit numeric values, not necessarily in increasing order, then all_values() will return the values in increasing order, but rf_enum.get_items() will return the items in the order of declaration.
For example, if we have:
type foo: [A=5, B, C=1, D];
then all_values(foo) will return C, D, A, B (in this order - according to the respective numeric values 1, 2, 5, 6), but rf_manager.get_type_by_name("foo").as_a(rf_enum).get_items() will return A, B, C, D.
If you want to get the reflection result sorted by the numeric values, you can do, for example, rf_manager.get_type_by_name("foo").as_a(rf_enum).get_items().sort(it.get_value()).
Another difference is that when we have several enumerated types based on each other, with different generative sub-ranges, then all_values() for a specific type will return only values that belong to that types's generative range, but rf_enum.get_items() will return all items.
For example:
type t1: [A, B, C, D];
type t2: t1[A..B];
type t3: t2;
extend t2: [E];
With this example, rf_enum.get_items() will return A, B, C, D, E for any of t1, t2, or t3. But all_values() will give different results, according to the generative range of each type. all_values(t1) will return A, B, C, D; all_values(t2) will return A, B, E; and all_values(t3) will return A, B.

Fast Fibonacci using recursion

I implemented a Fibonacci function in Scala and it works fine however when I enter 50 it takes a long time to compute it because it has to calculate the 2 previous integers each time. I found a function that keeps the 2 previous numbers. However, can somebody tell me how to write this function to make it accept 2 integers instead of 3 and return the last 2 numbers to compute the Fibonacci at a particular index x. Thanks!
def fastFib(x: Long ): Long = {
def fast(x:Long , a:Long, b:Long):Long =
if (x<=0) a+b
else fast(x-1,b,a+b)
if (x<2) 1
else fast(x-2,0,1)
}
You can cache the intermediate results, then you never recompute the same result twice
Here is the code
//this supposed to contains all the value when initialized
//initialized with 0 for all value
val cache = Array [Int] (101);//0 to 100
cache(1)==1;//initial value
cache(2)=1;//initial value
def fibonacciCache(n:Int) : Int = {
if (n>100)
{
println("error");
return -1;
}
if (cache(n)!=0)//means value has been calculated
return cache(n);
else
{
cache(n)=fibonacciCache(n-1)+fibonacciCache(n-2);
return cache(n);
}
}
Hope that helps