Sector Twelve

12.01 - int count = 0;
foreach(int n in numbers){
if(x == n)
count++;
}
return count;
12.02 - int count = 0;
foreach(string n in words){
if(n == s){
count++;
}
}
return count;
12.03 - return Array.IndexOf(numbers, x);

int j = 0;
for(int i = 0; i < numbers.Length; i++){
if(numbers[i] == x){
j = i;
break;
} else {
j = -1;
}
}
return j;
12.04 - return Array.LastIndexOf(numbers, x);
        int j = 0;
for(int i = numbers.Length - 1; i > -1; i--){

if(numbers[i] == x){
j = i;
break;
} else {
j = -1;
}
}
return j;
12.05 - using System;
using System.Linq;

public class Program {
    public static int[] Puzzle(int[] numbers, int x) {
int count = numbers.Count(a => a == x);
     

int []r = new int[count];
for(int i = 0, j = 0; i < numbers.Length; i++){
if(numbers[i] == x){
r[j] = i;
j++;
}
}
return r;
    }
}
12.06 - foreach(int n in numbers){
if(n == x){
numbers[Array.IndexOf(numbers, x)] = y;
}
}
return numbers;
12.07 - using System;
using System.Linq;
using System.Collections.Generic;
public class Program {
    public static int[][] Puzzle(int[] numbers, int x) {

List<int[]>r = new List<int[]>();
for(int i = 0; i < numbers.Length - 1; i++){
for(int s = i + 1; s < numbers.Length; s++){
if(numbers[i] + numbers[s] == x){
int [] a = new int [2];
a[0] = i;
a[1] = s;

r.Add(a);
}
}
}

return r.ToArray();
    }
}
12.08 - i don't know
12.09 - using System;
using System.Linq;
using System.Collections.Generic;
public class Program {
    public static bool Puzzle(int[] numbers) {
bool b =false;


for(int i = 0; i < numbers.Length - 1; i++){


if(numbers[i] > numbers[i + 1]){
b = false;
break;

}
if(numbers[i] < numbers[i + 1] || numbers[i] == numbers[i+1]){
b = true;
}

}
return b;
    }
}
12.10 - using System;
using System.Linq;
public class Program {
    public static bool Puzzle(string[] words) {

bool b = false;
        string [] r = new string [words.Length];
Array.Copy(words, r, words.Length);
Array.Sort(r);
return Enumerable.SequenceEqual(words, r);

    }
}
12.11 - Array.Sort(a);
return a;
12.12 - Array.Sort(a);
return a;

1 comment:

  1. // 12.08
    using System;
    public class Program {
    public static int Puzzle(int[] numbers) {
    int sum = 0;
    foreach (int num in numbers) {
    sum += num;
    }

    int prefixSum = 0;
    for (int i = 0; i < numbers.Length; i++) {
    prefixSum += numbers[i];
    if (prefixSum * 2 == sum) {
    return i;
    }
    }
    return -1;
    }
    }

    ReplyDelete