Codility's Permutation Check in C#Perm-Missing-Elem: 100% functional score, but only 60% performanceFind the...
What is this end portal thingy?
How can the dynamic linker/loader itself be dynamically linked as reported by `file`?
My machine, client installed VPN,
Calculate the Ultraradical
what organs or modifications would be needed to have hairy fish?
Why is a road bike faster than a city bike with the same effort? How much faster it can be?
A famous scholar sent me an unpublished draft of hers. Then she died. I think her work should be published. What should I do?
Can a passenger predict that an airline is about to go bankrupt?
Why does my browser attempt to download pages from http://clhs.lisp.se instead of viewing them normally?
What was Han searching through when he found where Lando was?
Does the app TikTok violate trademark?
How do we know neutrons have no charge?
Installation of ImageMagick software fails with "configure: error: libltdl is required"
What are examples of EU policies that are beneficial for one EU country, disadvantagious for another?
Selection Sort Algorithm (Python)
Top off gas with old oil, is that bad?
Can I build a half bath without permits?
"until mine is on tight" is a idiom?
What can Thomas Cook customers who have not yet departed do now it has stopped operating?
How do my husband and I get over our fear of having another difficult baby?
Is population size a parameter, or sample size a statistic?
How to prevent pickpocketing in busy bars?
Why isn't there armor to protect from spells in the Potterverse?
"I will not" or "I don't" as an answer for negative orders?
Codility's Permutation Check in C#
Perm-Missing-Elem: 100% functional score, but only 60% performanceFind the odd occurrences in an arrayRotate an array to the right by a given number of stepsPermMissingElem- find the missing element in a given permutation in C#Finding an equilibrium index in an int arrayCodility “PermMissingElem” SolutionPermCheck CodilityCodility Cyclic rotation in C#
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
$begingroup$
I've made a few things in Java, and now I'm learning C#.
The code passes the tests with 100% final score.
I want to know what things can be improved in my code.
Task description
A non-empty array A consisting of N integers is
given.
A permutation is a sequence containing each element from 1 to N once,
and only once.
For example, array A such that:
A[0] = 4
A[1] = 1
A[2] = 3
A[3] = 2 is a permutation, but array A such that:
A[0] = 4
A[1] = 1
A[2] = 3 is not a permutation, because value 2 is missing.
The goal is to check whether array A is a permutation.
Write a function:
class Solution { public int solution(int[] A); }
that, given an array A, returns 1 if array A is a permutation and 0 if
it is not.
For example, given array A such that:
A[0] = 4
A[1] = 1
A[2] = 3
A[3] = 2 the function should return 1.
Given array A such that:
A[0] = 4
A[1] = 1
A[2] = 3 the function should return 0.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [1..100,000]; each element of array A
is an integer within the range [1..1,000,000,000].
public static int solution(int[] A)
{
int[] orderedPermut = new int[A.Length];
int[] unrepeated = new int[A.Length];
int orderedPermutSum = 0, unrepeatedSum = 0;
for ( int i = 0; i < A.Length; i++ )
{
orderedPermutSum += i + 1;
orderedPermut[i] = i + 1;
if ( A[i] >= A.Length + 1 || unrepeated[ A[i] - 1 ] != 0 )
/*number is greater than A's length, or it's repeated,
/*therefore,A's not a permutation.*/
return 0;
else
{
unrepeated[A[i] - 1] = A[i];
unrepeatedSum += A[i];
}
}
if ( orderedPermutSum == unrepeatedSum )
{
return 1;
}
return 0;
}
Thanks in advice.
c# beginner programming-challenge array combinatorics
$endgroup$
add a comment
|
$begingroup$
I've made a few things in Java, and now I'm learning C#.
The code passes the tests with 100% final score.
I want to know what things can be improved in my code.
Task description
A non-empty array A consisting of N integers is
given.
A permutation is a sequence containing each element from 1 to N once,
and only once.
For example, array A such that:
A[0] = 4
A[1] = 1
A[2] = 3
A[3] = 2 is a permutation, but array A such that:
A[0] = 4
A[1] = 1
A[2] = 3 is not a permutation, because value 2 is missing.
The goal is to check whether array A is a permutation.
Write a function:
class Solution { public int solution(int[] A); }
that, given an array A, returns 1 if array A is a permutation and 0 if
it is not.
For example, given array A such that:
A[0] = 4
A[1] = 1
A[2] = 3
A[3] = 2 the function should return 1.
Given array A such that:
A[0] = 4
A[1] = 1
A[2] = 3 the function should return 0.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [1..100,000]; each element of array A
is an integer within the range [1..1,000,000,000].
public static int solution(int[] A)
{
int[] orderedPermut = new int[A.Length];
int[] unrepeated = new int[A.Length];
int orderedPermutSum = 0, unrepeatedSum = 0;
for ( int i = 0; i < A.Length; i++ )
{
orderedPermutSum += i + 1;
orderedPermut[i] = i + 1;
if ( A[i] >= A.Length + 1 || unrepeated[ A[i] - 1 ] != 0 )
/*number is greater than A's length, or it's repeated,
/*therefore,A's not a permutation.*/
return 0;
else
{
unrepeated[A[i] - 1] = A[i];
unrepeatedSum += A[i];
}
}
if ( orderedPermutSum == unrepeatedSum )
{
return 1;
}
return 0;
}
Thanks in advice.
c# beginner programming-challenge array combinatorics
$endgroup$
add a comment
|
$begingroup$
I've made a few things in Java, and now I'm learning C#.
The code passes the tests with 100% final score.
I want to know what things can be improved in my code.
Task description
A non-empty array A consisting of N integers is
given.
A permutation is a sequence containing each element from 1 to N once,
and only once.
For example, array A such that:
A[0] = 4
A[1] = 1
A[2] = 3
A[3] = 2 is a permutation, but array A such that:
A[0] = 4
A[1] = 1
A[2] = 3 is not a permutation, because value 2 is missing.
The goal is to check whether array A is a permutation.
Write a function:
class Solution { public int solution(int[] A); }
that, given an array A, returns 1 if array A is a permutation and 0 if
it is not.
For example, given array A such that:
A[0] = 4
A[1] = 1
A[2] = 3
A[3] = 2 the function should return 1.
Given array A such that:
A[0] = 4
A[1] = 1
A[2] = 3 the function should return 0.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [1..100,000]; each element of array A
is an integer within the range [1..1,000,000,000].
public static int solution(int[] A)
{
int[] orderedPermut = new int[A.Length];
int[] unrepeated = new int[A.Length];
int orderedPermutSum = 0, unrepeatedSum = 0;
for ( int i = 0; i < A.Length; i++ )
{
orderedPermutSum += i + 1;
orderedPermut[i] = i + 1;
if ( A[i] >= A.Length + 1 || unrepeated[ A[i] - 1 ] != 0 )
/*number is greater than A's length, or it's repeated,
/*therefore,A's not a permutation.*/
return 0;
else
{
unrepeated[A[i] - 1] = A[i];
unrepeatedSum += A[i];
}
}
if ( orderedPermutSum == unrepeatedSum )
{
return 1;
}
return 0;
}
Thanks in advice.
c# beginner programming-challenge array combinatorics
$endgroup$
I've made a few things in Java, and now I'm learning C#.
The code passes the tests with 100% final score.
I want to know what things can be improved in my code.
Task description
A non-empty array A consisting of N integers is
given.
A permutation is a sequence containing each element from 1 to N once,
and only once.
For example, array A such that:
A[0] = 4
A[1] = 1
A[2] = 3
A[3] = 2 is a permutation, but array A such that:
A[0] = 4
A[1] = 1
A[2] = 3 is not a permutation, because value 2 is missing.
The goal is to check whether array A is a permutation.
Write a function:
class Solution { public int solution(int[] A); }
that, given an array A, returns 1 if array A is a permutation and 0 if
it is not.
For example, given array A such that:
A[0] = 4
A[1] = 1
A[2] = 3
A[3] = 2 the function should return 1.
Given array A such that:
A[0] = 4
A[1] = 1
A[2] = 3 the function should return 0.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [1..100,000]; each element of array A
is an integer within the range [1..1,000,000,000].
public static int solution(int[] A)
{
int[] orderedPermut = new int[A.Length];
int[] unrepeated = new int[A.Length];
int orderedPermutSum = 0, unrepeatedSum = 0;
for ( int i = 0; i < A.Length; i++ )
{
orderedPermutSum += i + 1;
orderedPermut[i] = i + 1;
if ( A[i] >= A.Length + 1 || unrepeated[ A[i] - 1 ] != 0 )
/*number is greater than A's length, or it's repeated,
/*therefore,A's not a permutation.*/
return 0;
else
{
unrepeated[A[i] - 1] = A[i];
unrepeatedSum += A[i];
}
}
if ( orderedPermutSum == unrepeatedSum )
{
return 1;
}
return 0;
}
Thanks in advice.
c# beginner programming-challenge array combinatorics
c# beginner programming-challenge array combinatorics
edited 9 hours ago
dfhwze
12.6k3 gold badges22 silver badges90 bronze badges
12.6k3 gold badges22 silver badges90 bronze badges
asked 9 hours ago
newbienewbie
19910 bronze badges
19910 bronze badges
add a comment
|
add a comment
|
2 Answers
2
active
oldest
votes
$begingroup$
One of the unfortunate things you'll notice about coding challenges is that the requirements often force you to write code that isn't ideal. I'm going to call these out, even though in this case they're outside of your control.
Silly requirement: The method is named solution
. This name tells us nothing about the behavior of the function. Also (although less importantly), it goes against the C# method naming convention of PascalCase
. A much better name would be something like IsPermutation
.
Silly requirement: The parameter is named A
. Single-letter variable names are almost never a good idea. Also (although less importantly), it goes against the C# parameter naming convention of camelCase
. It's hard to be descriptive about the context here (since it's just a coding challenge and not a "real life" business problem), but even something like values
would be better.
Silly requirement: The return type of the function is int
. The given array is either a permutation of 1..N or it isn't. We don't have a range of possible return values; we have a true/false condition. A better return type would be bool
.
Algorithm inefficiency: There's no need to initialize two new int[]
. As you discovered, one new array is sufficient to track unique values.
Algorithm inefficiency: Again, you've already discovered this. If an array has the correct elements in some order, its sum will necessarily match that of the unordered array. Meanwhile the converse is not true; there are many arrays with the same sum which are not permutations. So you needn't bother tracking the sum at all.
Syntax: If you take only one piece of my advice, let it be this one: for loops are not the answer. If you are iterating over the indexes of an array, and the only thing you use the index for is to grab the ith
element, use a foreach. The situation where you actually need to know the index, and therefore need a for loop, is very rare.
Semantics: Does an array of int
take less space in memory than an array of bool
? I don't know, and I don't care. If all I'm tracking is true/false values, I'm going to choose a bool[]
. Because that choice tells the person reading the code why I created the variable.
Formatting: Another major difference between formatting conventions in C# and Java (besides the PascalCase/camelCase conventions I already mentioned) is C#'s convention of putting opening curly braces on the next line (known as K&R style).
With all of that advice applied, and some XML documentation comments (which enable Intellisense information when hovering or typing in Visual Studio), we'll end up with something like this:
/// <summary>
/// Check whether the given array contains each integer 1..N exactly once.
/// </summary>
/// <returns>
/// True if <paramref name="values" /> is a permutation of 1..N,
/// False otherwise.
/// </returns>
public static bool IsPermutation(int[] values)
{
var seen = new bool[values.length];
foreach (var value in values)
{
if (value < 1 || value > values.length)
{
// Out of range: not a permutation
return false;
}
else if (seen[value - 1])
{
// Duplicated value: not a permutation
return false;
}
else
{
// Value is OK. Mark as seen.
seen[value - 1] = true;
}
}
// All values in range, no duplicates: a valid permutation
return true;
}
$endgroup$
2
$begingroup$
I asked a general question about this kind of reviews where you challenge the challenge itself :) -> codereview.meta.stackexchange.com/questions/9345/…
$endgroup$
– dfhwze
6 hours ago
1
$begingroup$
Good idea. My thinking was to bridge the gap between a good solution to the challenge, and good "real life" code. I'm interested in how valuable the community finds that type of feedback.
$endgroup$
– benj2240
6 hours ago
$begingroup$
So am I. I've also commented on challenge requirements in some of my reviews, but never to this extend. I find it an interesting way of reviewing :)
$endgroup$
– dfhwze
6 hours ago
add a comment
|
$begingroup$
I checked the solution to this problem in codesays (answered by Sheng).
One thing I can improve from his better solution is that he followed the same approach, but didn't check if the sum of a proper and ordered permutation was correct, so he didn't use an ordered array.
I used two counters, one that I knew was correct, and another one that could go wrong, but if I think about it, if no elements were repeated, nor out of range, then checking for the sum to be right wasn't necessary, because it must be a permutation if that was the case. Checking for negative numbers was something I could also do, and then decide it was not a permutation, no tests tried arrays with negative numbers, though
class Solution {
public static int solution(int[] A) {
int[] counter = new int [A.length];
for(int i= 0; i< A.length; i++){
if (A[i] < 1 || A[i] > A.length) {
// Out of range
return 0;
}
else if(counter[A[i]-1] == 1) {
// met before
return 0;
}
else {
// first time meet
counter[A[i]-1] = 1;
}
}
return 1;
}
}
$endgroup$
add a comment
|
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f229525%2fcodilitys-permutation-check-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
One of the unfortunate things you'll notice about coding challenges is that the requirements often force you to write code that isn't ideal. I'm going to call these out, even though in this case they're outside of your control.
Silly requirement: The method is named solution
. This name tells us nothing about the behavior of the function. Also (although less importantly), it goes against the C# method naming convention of PascalCase
. A much better name would be something like IsPermutation
.
Silly requirement: The parameter is named A
. Single-letter variable names are almost never a good idea. Also (although less importantly), it goes against the C# parameter naming convention of camelCase
. It's hard to be descriptive about the context here (since it's just a coding challenge and not a "real life" business problem), but even something like values
would be better.
Silly requirement: The return type of the function is int
. The given array is either a permutation of 1..N or it isn't. We don't have a range of possible return values; we have a true/false condition. A better return type would be bool
.
Algorithm inefficiency: There's no need to initialize two new int[]
. As you discovered, one new array is sufficient to track unique values.
Algorithm inefficiency: Again, you've already discovered this. If an array has the correct elements in some order, its sum will necessarily match that of the unordered array. Meanwhile the converse is not true; there are many arrays with the same sum which are not permutations. So you needn't bother tracking the sum at all.
Syntax: If you take only one piece of my advice, let it be this one: for loops are not the answer. If you are iterating over the indexes of an array, and the only thing you use the index for is to grab the ith
element, use a foreach. The situation where you actually need to know the index, and therefore need a for loop, is very rare.
Semantics: Does an array of int
take less space in memory than an array of bool
? I don't know, and I don't care. If all I'm tracking is true/false values, I'm going to choose a bool[]
. Because that choice tells the person reading the code why I created the variable.
Formatting: Another major difference between formatting conventions in C# and Java (besides the PascalCase/camelCase conventions I already mentioned) is C#'s convention of putting opening curly braces on the next line (known as K&R style).
With all of that advice applied, and some XML documentation comments (which enable Intellisense information when hovering or typing in Visual Studio), we'll end up with something like this:
/// <summary>
/// Check whether the given array contains each integer 1..N exactly once.
/// </summary>
/// <returns>
/// True if <paramref name="values" /> is a permutation of 1..N,
/// False otherwise.
/// </returns>
public static bool IsPermutation(int[] values)
{
var seen = new bool[values.length];
foreach (var value in values)
{
if (value < 1 || value > values.length)
{
// Out of range: not a permutation
return false;
}
else if (seen[value - 1])
{
// Duplicated value: not a permutation
return false;
}
else
{
// Value is OK. Mark as seen.
seen[value - 1] = true;
}
}
// All values in range, no duplicates: a valid permutation
return true;
}
$endgroup$
2
$begingroup$
I asked a general question about this kind of reviews where you challenge the challenge itself :) -> codereview.meta.stackexchange.com/questions/9345/…
$endgroup$
– dfhwze
6 hours ago
1
$begingroup$
Good idea. My thinking was to bridge the gap between a good solution to the challenge, and good "real life" code. I'm interested in how valuable the community finds that type of feedback.
$endgroup$
– benj2240
6 hours ago
$begingroup$
So am I. I've also commented on challenge requirements in some of my reviews, but never to this extend. I find it an interesting way of reviewing :)
$endgroup$
– dfhwze
6 hours ago
add a comment
|
$begingroup$
One of the unfortunate things you'll notice about coding challenges is that the requirements often force you to write code that isn't ideal. I'm going to call these out, even though in this case they're outside of your control.
Silly requirement: The method is named solution
. This name tells us nothing about the behavior of the function. Also (although less importantly), it goes against the C# method naming convention of PascalCase
. A much better name would be something like IsPermutation
.
Silly requirement: The parameter is named A
. Single-letter variable names are almost never a good idea. Also (although less importantly), it goes against the C# parameter naming convention of camelCase
. It's hard to be descriptive about the context here (since it's just a coding challenge and not a "real life" business problem), but even something like values
would be better.
Silly requirement: The return type of the function is int
. The given array is either a permutation of 1..N or it isn't. We don't have a range of possible return values; we have a true/false condition. A better return type would be bool
.
Algorithm inefficiency: There's no need to initialize two new int[]
. As you discovered, one new array is sufficient to track unique values.
Algorithm inefficiency: Again, you've already discovered this. If an array has the correct elements in some order, its sum will necessarily match that of the unordered array. Meanwhile the converse is not true; there are many arrays with the same sum which are not permutations. So you needn't bother tracking the sum at all.
Syntax: If you take only one piece of my advice, let it be this one: for loops are not the answer. If you are iterating over the indexes of an array, and the only thing you use the index for is to grab the ith
element, use a foreach. The situation where you actually need to know the index, and therefore need a for loop, is very rare.
Semantics: Does an array of int
take less space in memory than an array of bool
? I don't know, and I don't care. If all I'm tracking is true/false values, I'm going to choose a bool[]
. Because that choice tells the person reading the code why I created the variable.
Formatting: Another major difference between formatting conventions in C# and Java (besides the PascalCase/camelCase conventions I already mentioned) is C#'s convention of putting opening curly braces on the next line (known as K&R style).
With all of that advice applied, and some XML documentation comments (which enable Intellisense information when hovering or typing in Visual Studio), we'll end up with something like this:
/// <summary>
/// Check whether the given array contains each integer 1..N exactly once.
/// </summary>
/// <returns>
/// True if <paramref name="values" /> is a permutation of 1..N,
/// False otherwise.
/// </returns>
public static bool IsPermutation(int[] values)
{
var seen = new bool[values.length];
foreach (var value in values)
{
if (value < 1 || value > values.length)
{
// Out of range: not a permutation
return false;
}
else if (seen[value - 1])
{
// Duplicated value: not a permutation
return false;
}
else
{
// Value is OK. Mark as seen.
seen[value - 1] = true;
}
}
// All values in range, no duplicates: a valid permutation
return true;
}
$endgroup$
2
$begingroup$
I asked a general question about this kind of reviews where you challenge the challenge itself :) -> codereview.meta.stackexchange.com/questions/9345/…
$endgroup$
– dfhwze
6 hours ago
1
$begingroup$
Good idea. My thinking was to bridge the gap between a good solution to the challenge, and good "real life" code. I'm interested in how valuable the community finds that type of feedback.
$endgroup$
– benj2240
6 hours ago
$begingroup$
So am I. I've also commented on challenge requirements in some of my reviews, but never to this extend. I find it an interesting way of reviewing :)
$endgroup$
– dfhwze
6 hours ago
add a comment
|
$begingroup$
One of the unfortunate things you'll notice about coding challenges is that the requirements often force you to write code that isn't ideal. I'm going to call these out, even though in this case they're outside of your control.
Silly requirement: The method is named solution
. This name tells us nothing about the behavior of the function. Also (although less importantly), it goes against the C# method naming convention of PascalCase
. A much better name would be something like IsPermutation
.
Silly requirement: The parameter is named A
. Single-letter variable names are almost never a good idea. Also (although less importantly), it goes against the C# parameter naming convention of camelCase
. It's hard to be descriptive about the context here (since it's just a coding challenge and not a "real life" business problem), but even something like values
would be better.
Silly requirement: The return type of the function is int
. The given array is either a permutation of 1..N or it isn't. We don't have a range of possible return values; we have a true/false condition. A better return type would be bool
.
Algorithm inefficiency: There's no need to initialize two new int[]
. As you discovered, one new array is sufficient to track unique values.
Algorithm inefficiency: Again, you've already discovered this. If an array has the correct elements in some order, its sum will necessarily match that of the unordered array. Meanwhile the converse is not true; there are many arrays with the same sum which are not permutations. So you needn't bother tracking the sum at all.
Syntax: If you take only one piece of my advice, let it be this one: for loops are not the answer. If you are iterating over the indexes of an array, and the only thing you use the index for is to grab the ith
element, use a foreach. The situation where you actually need to know the index, and therefore need a for loop, is very rare.
Semantics: Does an array of int
take less space in memory than an array of bool
? I don't know, and I don't care. If all I'm tracking is true/false values, I'm going to choose a bool[]
. Because that choice tells the person reading the code why I created the variable.
Formatting: Another major difference between formatting conventions in C# and Java (besides the PascalCase/camelCase conventions I already mentioned) is C#'s convention of putting opening curly braces on the next line (known as K&R style).
With all of that advice applied, and some XML documentation comments (which enable Intellisense information when hovering or typing in Visual Studio), we'll end up with something like this:
/// <summary>
/// Check whether the given array contains each integer 1..N exactly once.
/// </summary>
/// <returns>
/// True if <paramref name="values" /> is a permutation of 1..N,
/// False otherwise.
/// </returns>
public static bool IsPermutation(int[] values)
{
var seen = new bool[values.length];
foreach (var value in values)
{
if (value < 1 || value > values.length)
{
// Out of range: not a permutation
return false;
}
else if (seen[value - 1])
{
// Duplicated value: not a permutation
return false;
}
else
{
// Value is OK. Mark as seen.
seen[value - 1] = true;
}
}
// All values in range, no duplicates: a valid permutation
return true;
}
$endgroup$
One of the unfortunate things you'll notice about coding challenges is that the requirements often force you to write code that isn't ideal. I'm going to call these out, even though in this case they're outside of your control.
Silly requirement: The method is named solution
. This name tells us nothing about the behavior of the function. Also (although less importantly), it goes against the C# method naming convention of PascalCase
. A much better name would be something like IsPermutation
.
Silly requirement: The parameter is named A
. Single-letter variable names are almost never a good idea. Also (although less importantly), it goes against the C# parameter naming convention of camelCase
. It's hard to be descriptive about the context here (since it's just a coding challenge and not a "real life" business problem), but even something like values
would be better.
Silly requirement: The return type of the function is int
. The given array is either a permutation of 1..N or it isn't. We don't have a range of possible return values; we have a true/false condition. A better return type would be bool
.
Algorithm inefficiency: There's no need to initialize two new int[]
. As you discovered, one new array is sufficient to track unique values.
Algorithm inefficiency: Again, you've already discovered this. If an array has the correct elements in some order, its sum will necessarily match that of the unordered array. Meanwhile the converse is not true; there are many arrays with the same sum which are not permutations. So you needn't bother tracking the sum at all.
Syntax: If you take only one piece of my advice, let it be this one: for loops are not the answer. If you are iterating over the indexes of an array, and the only thing you use the index for is to grab the ith
element, use a foreach. The situation where you actually need to know the index, and therefore need a for loop, is very rare.
Semantics: Does an array of int
take less space in memory than an array of bool
? I don't know, and I don't care. If all I'm tracking is true/false values, I'm going to choose a bool[]
. Because that choice tells the person reading the code why I created the variable.
Formatting: Another major difference between formatting conventions in C# and Java (besides the PascalCase/camelCase conventions I already mentioned) is C#'s convention of putting opening curly braces on the next line (known as K&R style).
With all of that advice applied, and some XML documentation comments (which enable Intellisense information when hovering or typing in Visual Studio), we'll end up with something like this:
/// <summary>
/// Check whether the given array contains each integer 1..N exactly once.
/// </summary>
/// <returns>
/// True if <paramref name="values" /> is a permutation of 1..N,
/// False otherwise.
/// </returns>
public static bool IsPermutation(int[] values)
{
var seen = new bool[values.length];
foreach (var value in values)
{
if (value < 1 || value > values.length)
{
// Out of range: not a permutation
return false;
}
else if (seen[value - 1])
{
// Duplicated value: not a permutation
return false;
}
else
{
// Value is OK. Mark as seen.
seen[value - 1] = true;
}
}
// All values in range, no duplicates: a valid permutation
return true;
}
edited 6 hours ago
answered 6 hours ago
benj2240benj2240
9463 silver badges9 bronze badges
9463 silver badges9 bronze badges
2
$begingroup$
I asked a general question about this kind of reviews where you challenge the challenge itself :) -> codereview.meta.stackexchange.com/questions/9345/…
$endgroup$
– dfhwze
6 hours ago
1
$begingroup$
Good idea. My thinking was to bridge the gap between a good solution to the challenge, and good "real life" code. I'm interested in how valuable the community finds that type of feedback.
$endgroup$
– benj2240
6 hours ago
$begingroup$
So am I. I've also commented on challenge requirements in some of my reviews, but never to this extend. I find it an interesting way of reviewing :)
$endgroup$
– dfhwze
6 hours ago
add a comment
|
2
$begingroup$
I asked a general question about this kind of reviews where you challenge the challenge itself :) -> codereview.meta.stackexchange.com/questions/9345/…
$endgroup$
– dfhwze
6 hours ago
1
$begingroup$
Good idea. My thinking was to bridge the gap between a good solution to the challenge, and good "real life" code. I'm interested in how valuable the community finds that type of feedback.
$endgroup$
– benj2240
6 hours ago
$begingroup$
So am I. I've also commented on challenge requirements in some of my reviews, but never to this extend. I find it an interesting way of reviewing :)
$endgroup$
– dfhwze
6 hours ago
2
2
$begingroup$
I asked a general question about this kind of reviews where you challenge the challenge itself :) -> codereview.meta.stackexchange.com/questions/9345/…
$endgroup$
– dfhwze
6 hours ago
$begingroup$
I asked a general question about this kind of reviews where you challenge the challenge itself :) -> codereview.meta.stackexchange.com/questions/9345/…
$endgroup$
– dfhwze
6 hours ago
1
1
$begingroup$
Good idea. My thinking was to bridge the gap between a good solution to the challenge, and good "real life" code. I'm interested in how valuable the community finds that type of feedback.
$endgroup$
– benj2240
6 hours ago
$begingroup$
Good idea. My thinking was to bridge the gap between a good solution to the challenge, and good "real life" code. I'm interested in how valuable the community finds that type of feedback.
$endgroup$
– benj2240
6 hours ago
$begingroup$
So am I. I've also commented on challenge requirements in some of my reviews, but never to this extend. I find it an interesting way of reviewing :)
$endgroup$
– dfhwze
6 hours ago
$begingroup$
So am I. I've also commented on challenge requirements in some of my reviews, but never to this extend. I find it an interesting way of reviewing :)
$endgroup$
– dfhwze
6 hours ago
add a comment
|
$begingroup$
I checked the solution to this problem in codesays (answered by Sheng).
One thing I can improve from his better solution is that he followed the same approach, but didn't check if the sum of a proper and ordered permutation was correct, so he didn't use an ordered array.
I used two counters, one that I knew was correct, and another one that could go wrong, but if I think about it, if no elements were repeated, nor out of range, then checking for the sum to be right wasn't necessary, because it must be a permutation if that was the case. Checking for negative numbers was something I could also do, and then decide it was not a permutation, no tests tried arrays with negative numbers, though
class Solution {
public static int solution(int[] A) {
int[] counter = new int [A.length];
for(int i= 0; i< A.length; i++){
if (A[i] < 1 || A[i] > A.length) {
// Out of range
return 0;
}
else if(counter[A[i]-1] == 1) {
// met before
return 0;
}
else {
// first time meet
counter[A[i]-1] = 1;
}
}
return 1;
}
}
$endgroup$
add a comment
|
$begingroup$
I checked the solution to this problem in codesays (answered by Sheng).
One thing I can improve from his better solution is that he followed the same approach, but didn't check if the sum of a proper and ordered permutation was correct, so he didn't use an ordered array.
I used two counters, one that I knew was correct, and another one that could go wrong, but if I think about it, if no elements were repeated, nor out of range, then checking for the sum to be right wasn't necessary, because it must be a permutation if that was the case. Checking for negative numbers was something I could also do, and then decide it was not a permutation, no tests tried arrays with negative numbers, though
class Solution {
public static int solution(int[] A) {
int[] counter = new int [A.length];
for(int i= 0; i< A.length; i++){
if (A[i] < 1 || A[i] > A.length) {
// Out of range
return 0;
}
else if(counter[A[i]-1] == 1) {
// met before
return 0;
}
else {
// first time meet
counter[A[i]-1] = 1;
}
}
return 1;
}
}
$endgroup$
add a comment
|
$begingroup$
I checked the solution to this problem in codesays (answered by Sheng).
One thing I can improve from his better solution is that he followed the same approach, but didn't check if the sum of a proper and ordered permutation was correct, so he didn't use an ordered array.
I used two counters, one that I knew was correct, and another one that could go wrong, but if I think about it, if no elements were repeated, nor out of range, then checking for the sum to be right wasn't necessary, because it must be a permutation if that was the case. Checking for negative numbers was something I could also do, and then decide it was not a permutation, no tests tried arrays with negative numbers, though
class Solution {
public static int solution(int[] A) {
int[] counter = new int [A.length];
for(int i= 0; i< A.length; i++){
if (A[i] < 1 || A[i] > A.length) {
// Out of range
return 0;
}
else if(counter[A[i]-1] == 1) {
// met before
return 0;
}
else {
// first time meet
counter[A[i]-1] = 1;
}
}
return 1;
}
}
$endgroup$
I checked the solution to this problem in codesays (answered by Sheng).
One thing I can improve from his better solution is that he followed the same approach, but didn't check if the sum of a proper and ordered permutation was correct, so he didn't use an ordered array.
I used two counters, one that I knew was correct, and another one that could go wrong, but if I think about it, if no elements were repeated, nor out of range, then checking for the sum to be right wasn't necessary, because it must be a permutation if that was the case. Checking for negative numbers was something I could also do, and then decide it was not a permutation, no tests tried arrays with negative numbers, though
class Solution {
public static int solution(int[] A) {
int[] counter = new int [A.length];
for(int i= 0; i< A.length; i++){
if (A[i] < 1 || A[i] > A.length) {
// Out of range
return 0;
}
else if(counter[A[i]-1] == 1) {
// met before
return 0;
}
else {
// first time meet
counter[A[i]-1] = 1;
}
}
return 1;
}
}
answered 9 hours ago
newbienewbie
19910 bronze badges
19910 bronze badges
add a comment
|
add a comment
|
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f229525%2fcodilitys-permutation-check-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown