I am trying to write a package in go. I am given two files:
I need to modify the function to return the min of an array of ints. do not change the first line of code. Please write in go or I will mark this answer as incorrect.
func Min(arr []int) int {
}
I am also given this tester code:
package min
import "testing"
func TestMin(t *testing.T) {
tests := []struct {
in []int
expected int
}{
{[]int{0, -1, 1, 2, -4}, -4},
{[]int{1}, 1},
{[]int{0}, 0},
{[]int{}, 0},
{nil, 0},
// TODO add more tests for 100%
test coverage
}
for i, test := range tests {
actual := Min(test.in)
if actual != test.expected {
t.Errorf("#%d:
Min(%v)=%d; expected %d", i, test.in, actual, test.expected)
}
}
}
Please find properly commented and working code below :
------------------------------------------------------------------
func Min(arr []int)int {
if arr == nil {
//if there is no array,
return 0
//return 0
}
if len(arr) == 0 {
//if array is empty,
return 0
//return 0
}
min := 0
min = arr[0]
//assume first element to be minumum
for _, v := range arr {
//Iterate over all elements in array
if (v < min) { //if found
something smaller than current minumum,
min = v //set it
as current minimum
}
}
return min
//return minimum
}
------------------------------------------------------------------
func TestMin(t *testing.T) {
tests := []struct {
in []int
expected int
}{
{[]int{0, -1, 1, 2, -4},
-4},
{[]int{1}, 1},
{[]int{0}, 0},
{[]int{}, 0},
{nil, 0},
{[]int{0, 51, 1, 2,
-0},0},
{[]int{70, 5, 10, 20,
55},5},
}
for i, test := range tests {
actual := Min(test.in)
if actual != test.expected
{
t.Errorf("#%d: Min(%v)=%d; expected %d", i, test.in, actual,
test.expected)
}
}
}
--------------------------------------------------------------------
Please find the screenshot for working code :
ere's the modified function in Go to return the minimum value of an array of integers:
goCopy codepackage minfunc Min(arr []int) int { if len(arr) == 0 { return 0 } min := arr[0] for _, num := range arr { if num < min { min = num } } return min }
This function iterates through the array and keeps track of the minimum value found so far. If the array is empty, it returns 0 as specified.
Now, you can use the provided tester code to test the Min
function and ensure it works correctly:
goCopy codepackage minimport "testing"func TestMin(t *testing.T) { tests := []struct { in []int expected int }{ {[]int{0, -1, 1, 2, -4}, -4}, {[]int{1}, 1}, {[]int{0}, 0}, {[]int{}, 0}, {nil, 0}, // TODO add more tests for 100% test coverage } for i, test := range tests { actual := Min(test.in) if actual != test.expected { t.Errorf("#%d: Min(%v)=%d; expected %d", i, test.in, actual, test.expected) } } }
Make sure to add more test cases to achieve 100% test coverage as indicated in the tester code.
I am trying to write a package in go. I am given two files: I need...
I am trying to write the following code using pointers and traversal by pointers instead of indexing (in C++). The following code is correct: void shift(int arr[], int n) { int temp = arr[n - 1]; int temp1; for (int i = 0; i < n; i++) { temp1 = arr[i]; arr[i] = temp; temp = temp1; } } This is my ATTEMPT at writing it with pointers (and traversal by pointers!) but I know it is wrong. I believe...
I am given an input file, P1input.txt and I have to write code to find the min and max, as well as prime and perfect numbers from the input file. P1input.txt contains a hundred integers. Why doesn't my code compile properly to show me all the numbers? It just stops and displays usage: C:\> java Project1 P1input.txt 1 30 import java.io.*; // BufferedReader import java.util.*; // Scanner to read from a text file public class Project1 { public static...
IMPLEMENT AN IMMUTABLE SINGLY LINKED LIST IN JAVASCRIPT ------------------------------------------------------------------------------------------------------------------------- So I am trying to implement a singly linked list that given the below function will pass the bottom four tests when ran. Please help me generate the singly linked list that will work and pass the tests. I will only use this as a reference so please add comments to explain what you are doing so I can understand and write this myself. I think a join method may need...
I am trying to implement this version of quicksort in C/c++ but I am getting stuck. Below is how the algorithm is supposed to work. This is the partitioning scheme I am trying to implement. 17, -10, 7, 19, 21, 23, -13, 31, 59 # ^ ^ start 17, -10, 7, 19, 21, 23, -13, 31, 59 # ^ ^ move left pointer to first element larger than pivot. 3 compares 17, -10, 7, 19, 21, 23, -13, 31, 59...
I am trying to write C programming code and output will be as below but I donno how to get the result analysis part. help me to add the 2nd part with my code: here is my code below: #include <stdio.h> #define MAX 100 struct studentMarkVariable{ int id; float marks; }; void getData(struct studentMarkVariable arrs[]); void show(struct studentMarkVariable arrs[]); int main() { printf ("####### Marks Analyzer V3.0 ####### \n"); struct studentMarkVariable arrs[MAX]; getData(arrs); show(arrs); return 0; }...
#include <assert.h> #include <stdio.h> #include <stdlib.h> // initialize_array is given an array "arr" of "n" elements. // It initializes the array by setting all elements to be "true" (any non-zero value). void initialize_array(int *arr, int n) { // TODO: Your code here. assert(0); } // mark_multiples is given an array "arr" of size n and a (prime) number "p" less than "n" // It assigns "false" (the zero value) to elements at array indexes 2*p, 3*p, 4*p,.., x*p (where x*p...
I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...
Assume that arr[] is an array of 5 values. Write code within the FOR loop below so that it finds the min value in the array. You MUST use the x pointer within the FOR loop. It is the only pointer you can use. You are not allowed to made additional function calls or modify anything else within the function. Do not modify the FOR loop conditions or anything outside of the FOR loop. You must use complete C++ code...
I am using python3.6 and i am getting an error on line 42 num = int(fin.readline()) #reading first value valueError: invalid literal, for int() with base 10 Any help is appreciated, here is the code. reads from txt file a few integers in an array and sorts them. thank you! # Function to do insertion sort def insertionSort(arr): # Traverse through 1 to len(arr) for i in range(1, len(arr)): key = arr[i] # Move elements of...
I need help to write a test case. 1. Go to the ConcertTicketTest.java file and write test cases for the compareTo method you just implemented. /* ConcertTicket.java file */ package SearchingSorting; /** * * @author clatulip */ public class ConcertTicket implements Comparable<ConcertTicket> { private String name; private int price; private char row; private int seat; public ConcertTicket(String name, int price) { this.name = name; this.price = price; // randomly create a row and seat // assumes 60 seats across width...