How do I sort a list of Doubles in Scala? [duplicate] - scala

This question already has answers here:
How do I sort an array in Scala?
(7 answers)
Closed 8 years ago.
How do I sort a simple list of Doubles in Scala?
var dubs = List(1.3,4.5,2.3,3.2)
I think my question may not have accurately reflected my specific problem, since I realize now that dubs.sorted will work just fine for the above. My problem is as follows, I have a string of doubles "2.3 32.4 54.2 1.33" that I'm parsing and adding to a list
var numsAsStrings = l.split("\\s");
var x = List(Double);
var i = 0;
for( i <- 0 until numsAsStrings.length) {
x :+ numsAsStrings(i).toDouble;
}
So, I would think that I could just call x.sorted on the above, but that doesn't work... I've been looking over the sortBy, sorted, and sortWith documentation and various posts, but I thought the solution should be simpler. I think I'm missing something basic, regardless.

Use the sorted method
dubs.sorted // List(1.3, 2.3, 3.2, 4.5)

Related

How to stop rounding in NSDecimalNumber? [duplicate]

This question already has answers here:
In Swift 3, how to calculate the factorial when the result becomes too high?
(2 answers)
BigInteger equivalent in Swift?
(6 answers)
Closed 3 years ago.
I am solving a question from HackerRank which asks to print the value of extra-long factorials that can't be stored even in a 64-bit long variable.
I am using NSDecimalNumber to store the value. However, even in this case, the final result is rounded off.
func extraLongFactorials(n: Int) -> Void
{
var factorial: NSDecimalNumber = 1
for index in 1...n
{
let indexInNSDecimal = NSDecimalNumber(value: index)
factorial = factorial.multiplying(by: indexInNSDecimal)
}
let factorialWithoutRounding = factorial.description(withLocale: nil)
print(factorialWithoutRounding)
}
print(extraLongFactorials(n: 45)) // 119622220865480194561963161495657715064000000000000000000
However, the result should be 119622220865480194561963161495657715064383733760000000000.
This link talks about using description(withLocale:).
NSDecimalNumber round long numbers
However, it does not clearly explain how to use the description(withLocale:) method.
I also went through the apple doc https://developer.apple.com/documentation/foundation/nsdecimalnumber/1412789-description. But it also does not explain clearly how to use it.
Can someone please discuss this method in detail.

Get Distinct Members of an Array by property [duplicate]

This question already has answers here:
Removing Duplicates From Array of Custom Objects Swift
(4 answers)
Remove objects with duplicate properties from Swift array
(16 answers)
Closed 5 years ago.
I have an array
Contract object contains:
String:id
String:value
Array is:
contract1 = Contract.new()
contract1.id = 2
contract1.value = "Apple"
contract2 = Contract.new()
contract2.id = 2
contract2.value = "Pen"
contract3 = Contract.new()
contract3.id = 1
contract3.value = "Pineapple"
array = [Contract1, Contract2, Contract3]
I would to find out the list of contracts whose IDs are distinct.
I want to have a solution that doesn't make me change the implementation of my object (overriding the isEqual method etc) since I will be using it for more than one object through out my code.
Desired result:
[contract1, contract3] or [contract2, contract3]
Ideally, an extension with additionally a method to only return the values that are being made distinct:
Desired result: [2, 1]
I tried a couple of approaches from similar questions but either the answers are outdated or doesn't fit my need.

Faster way to create an array of numbers spanning a range in Swift [duplicate]

This question already has answers here:
Is there a way to instantly generate an array filled with a range of values in Swift?
(4 answers)
Closed 6 years ago.
Is there a shorter way to create an array of numbers spanning a range in swift?
Right now, I'm using this:
var arrayOfInts = [UInt32]()
for number in 1...100 {
arrayOfInts.append(number)
}
Is there a one-line way of doing it?
var arrayOfInts = Array(1...100)
Playground Output
Is this short enough?
let array = Array(1...100)
Try like this
var z = [Int](1...100)
print(z)
DEMO

Is there an easier way to get the lesser of two values in Swift [duplicate]

This question already has answers here:
Swift equivalent for MIN and MAX macros
(5 answers)
Closed 8 years ago.
I'd like to assign the lesser of two values to a variable. In Ruby I would do something like:
my_var = [value_one, value_two].min
In Swift, of course, I can do this:
var myVar = 0.0
if valueOne < valueTwo {
myVar = valueOne
} else {
myVar = valueTwo
}
But, I'm wondering if there is a cleaner, more succinct solution.
var myVar = min(valueOne, valueTwo)
min is a standard library function that takes the lesser of two (or least of several — it's variadic) Comparable values.

Scala 2 dimensional Array and Arrays [duplicate]

This question already has answers here:
How to create and use a multi-dimensional array in Scala?
(3 answers)
Closed 8 years ago.
I am trying to write my Java ode from in Scala and I think I need some help.
my problem:
Java:
public static int[][] ScoreMatrix = new int[5][20];
Scala:
var ScoreMatrix: Array[Array[Int]] = new Array[Array[Int]](5, 20)
It's not working, don't know why?
Error "too many arguments for constructor Array(_length:int)Array[Array[Int]]"
For initializing 5*20 2D int array you can use:
var ScoreMatrix: Array[Array[Int]] = Array.ofDim[Int](5, 20)
Your code doesn't work because the Array constructor has only one argument, which is the array length.
Consider also
Array.tabulate(5,20)( (x,y) => 1)
which instantiates a 5 by 20 array with Int: 1 (in general a function of x and y).