Skip to content

Commit

Permalink
Added normalizedArray and indexArray.
Browse files Browse the repository at this point in the history
  • Loading branch information
Olcay Taner YILDIZ committed Sep 3, 2022
1 parent b0fd9a4 commit 8cf0ad6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

import Foundation

public class RandomNormalizedArray{

private var array: [Double]
public class RandomArray{

/**
The constructor of RandomNormalizedArray class gets an integer itemCount as an input. Creates a list of
Expand All @@ -18,25 +16,26 @@ public class RandomNormalizedArray{

- Parameters itemCount : input representing array size.
*/
public init(itemCount: Int){
public static func normalizedArray(itemCount: Int) -> [Double]{
var total = 0.0
self.array = []
var array : [Double] = []
let random = Random()
for i in 0..<itemCount{
self.array.append(random.nextDouble(min: 0, max: 1))
total += self.array[i]
array.append(random.nextDouble(min: 0, max: 1))
total += array[i]
}
for i in 0..<itemCount{
self.array[i] /= total
array[i] /= total
}
return array
}

/**
Getter for the double list.

- Returns: the double list.
*/
public func get() -> [Double]{
return self.array
public static func indexArray(itemCount: Int) -> [Int]{
var array : [Int] = []
for i in 0..<itemCount{
array.append(i)
}
array.shuffle()
return array
}
}
28 changes: 28 additions & 0 deletions Tests/UtilTests/RandomArrayTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import XCTest
@testable import Util

final class RandomArrayTest: XCTestCase {

func testNormalizedArray() {
let array : [Double] = RandomArray.normalizedArray(itemCount: 10)
var sum : Double = 0.0
for d in array{
sum += d
}
XCTAssertEqual(1.0, sum)
}

func testIndexArray() {
let array : [Int] = RandomArray.indexArray(itemCount: 10)
var sum : Int = 0
for d in array{
sum += d
}
XCTAssertEqual(45, sum)
}

static var allTests = [
("testExample1", testNormalizedArray),
("testExample2", testIndexArray),
]
}
18 changes: 0 additions & 18 deletions Tests/UtilTests/RandomNormalizedArrayTest.swift

This file was deleted.

0 comments on commit 8cf0ad6

Please # to comment.