C
C#10mo ago
Shunrai

[Help] Find Evens or Odds

Bois, I require assisstance. The test platform for exercises in giving me 3/5 errors and I can't figure out why since they do not provide you with the issue. Do you have any ideas?
You are given a lower and an upper bound for a range of integer numbers. Then a command specifies if you need to list all even or odd numbers in the given range. Use Predicate<T>.
You are given a lower and an upper bound for a range of integer numbers. Then a command specifies if you need to list all even or odd numbers in the given range. Use Predicate<T>.
My Code:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace _04._Find_Evens_or_Odds
{
internal class Program
{
static void Main(string[] args)
{
int[] input = new int[2];
input = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
List<int> output = new List<int>();
string command = Console.ReadLine().Trim().ToLower();
Predicate<int> filter = command == "odd" ? new Predicate<int>(x => x % 2 == 1) : command == "even" ? new Predicate<int>(x => x % 2 == 0) : null;
Predicate<int> predicate = i => true;
for (int i = input[0]; i <= input[1]; i++)
{
if (filter(i))
output.Add(i);
}
Console.WriteLine(string.Join(" ", output));
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace _04._Find_Evens_or_Odds
{
internal class Program
{
static void Main(string[] args)
{
int[] input = new int[2];
input = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
List<int> output = new List<int>();
string command = Console.ReadLine().Trim().ToLower();
Predicate<int> filter = command == "odd" ? new Predicate<int>(x => x % 2 == 1) : command == "even" ? new Predicate<int>(x => x % 2 == 0) : null;
Predicate<int> predicate = i => true;
for (int i = input[0]; i <= input[1]; i++)
{
if (filter(i))
output.Add(i);
}
Console.WriteLine(string.Join(" ", output));
}
}
}
6 Replies
Shunrai
ShunraiOP10mo ago
Examples: 1 10 odd 1 3 5 7 9 20 30 even 20 22 24 26 28 30
canton7
canton710mo ago
Hard to say -- the output you've given matches the specification you've given Are you sure the upper bound is inclusive?
Shunrai
ShunraiOP10mo ago
figured it out apparatnly for example -5 % 2 = -1 DogeKek
canton7
canton710mo ago
Aah, negative values. Sneaky The spec says to use Predicate. Presumably this is an exercise around using delegates
i like chatgpt
i like chatgpt10mo ago
With Predicate.
Console.WriteLine("Enter two positive integers as interval boundaries.");
string? rawInput = Console.ReadLine();

// Note: pressing ctrl+z and enter makes ReadLine() returns null.
if (string.IsNullOrEmpty(rawInput))
throw new Exception("Input must not be null or empty.");


string[] texts = rawInput.Split(' ',
StringSplitOptions.RemoveEmptyEntries |
StringSplitOptions.TrimEntries);

if (texts.Length != 2)
throw new Exception("Input must be two positive integer separated by spaces");



if (!int.TryParse(texts[0], out int left) ||
!int.TryParse(texts[1], out int right))
throw new Exception("Input cannot be converted to numbers.");


if (left > right)
throw new Exception("The left integer cannot be greater than the right one.");


Console.WriteLine("Type odd or even for odd or even sequences.");
string? parity = Console.ReadLine()?.ToLower();
if (parity != "even" && parity != "odd")
throw new Exception("Parity must be either even or odd.");

Predicate<int> filter = parity == "odd" ?
x => x % 2 == 1 :
x => x % 2 == 0;

List<int> sequences = new();
while (left <= right)
{
if (filter(left)) sequences.Add(left);
left++;
}

Console.WriteLine(string.Join(", ", sequences));
Console.WriteLine("Enter two positive integers as interval boundaries.");
string? rawInput = Console.ReadLine();

// Note: pressing ctrl+z and enter makes ReadLine() returns null.
if (string.IsNullOrEmpty(rawInput))
throw new Exception("Input must not be null or empty.");


string[] texts = rawInput.Split(' ',
StringSplitOptions.RemoveEmptyEntries |
StringSplitOptions.TrimEntries);

if (texts.Length != 2)
throw new Exception("Input must be two positive integer separated by spaces");



if (!int.TryParse(texts[0], out int left) ||
!int.TryParse(texts[1], out int right))
throw new Exception("Input cannot be converted to numbers.");


if (left > right)
throw new Exception("The left integer cannot be greater than the right one.");


Console.WriteLine("Type odd or even for odd or even sequences.");
string? parity = Console.ReadLine()?.ToLower();
if (parity != "even" && parity != "odd")
throw new Exception("Parity must be either even or odd.");

Predicate<int> filter = parity == "odd" ?
x => x % 2 == 1 :
x => x % 2 == 0;

List<int> sequences = new();
while (left <= right)
{
if (filter(left)) sequences.Add(left);
left++;
}

Console.WriteLine(string.Join(", ", sequences));
canton7
canton710mo ago
The line to change left seems unncessary? The first time through the loop will sort that out. It's also got the same bug that OP's had It'd also be cleaner to use a for loop with a loop variable IMO, rather than mutating one of the bounds (which is what OP did)
Want results from more Discord servers?
Add your server