Prints each letter of a string in different colors. C#Counting the occurrence of each letter in a stringComparing different string-matching functionsGenerating Unique ColorsFinding the maximum pairwise difference in a collection of colorsExtension method splitting string on each capital letterComparing XML string with different formatsIncrementing colorsGenerating an image with all 15-bit colors, each used exactly onceUpdate SQL database, similar-but-different queries for each monthReturn Full Month Name From 3 Letter Month
Hotel booking: Why is Agoda much cheaper than booking.com?
Can 2 light bulbs of 120V in series be used on 230V AC?
How do you cope with rejection?
What color to choose as "danger" if the main color of my app is red
Why aren't satellites disintegrated even though they orbit earth within earth's Roche Limits?
How to customize the pie chart background in PowerPoint?
Can more than one instance of Bend Luck be applied to the same roll by multiple Wild Magic sorcerers?
Largest memory peripheral for Sinclair ZX81?
Is it standard to have the first week's pay indefinitely withheld?
Why do academics prefer Mac/Linux?
Why is choosing a suitable thermodynamic potential important?
Gaussian kernel density estimation with data from file
Should I twist DC power and ground wires from a power supply?
French equivalent of the German expression "flöten gehen"
Told to apply for UK visa before other visas
Why is Drogon so much better in battle than Rhaegal and Viserion?
Pedaling at different gear ratios on flat terrain: what's the point?
Does the talk count as invited if my PI invited me?
Was Tyrion always a poor strategist?
Are there any symmetric cryptosystems based on computational complexity assumptions?
Using `printf` to print variable containing `%` percent sign results in "bash: printf: `p': invalid format character"
Who is frowning in the sentence "Daisy looked at Tom frowning"?
Combining two Lorentz boosts
Why does Taylor’s series “work”?
Prints each letter of a string in different colors. C#
Counting the occurrence of each letter in a stringComparing different string-matching functionsGenerating Unique ColorsFinding the maximum pairwise difference in a collection of colorsExtension method splitting string on each capital letterComparing XML string with different formatsIncrementing colorsGenerating an image with all 15-bit colors, each used exactly onceUpdate SQL database, similar-but-different queries for each monthReturn Full Month Name From 3 Letter Month
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
I'm practicing for my Technical Interview and I'm worried my code does not meet the bar. What can I do to improve this answer?
Here is the question: https://www.careercup.com/question?id=5739126414901248
Given
colors = ["red", "blue", "green", "yellow"];
and a string
str = "Lorem ipsum dolor sit amet";
write a function that prints each letter of the string in different colors. ex. L
is red, o
is blue, r
is green, e
is yellow, m
is red, after the space, i
should be blue.
My answer:
static void TestPrintColors()
string[] colors = new string[4] "red", "blue", "green", "yellow";
string str = "Lorem ipsum dolor sit amet";
PrintColors(colors, str);
static void PrintColors(string[] colors, string str)
char log;
ConsoleColor originalColor = Console.ForegroundColor;
int colorIndex = 0;
ConsoleColor currentColor = originalColor;
for (int i = 0; i < str.Length; i++)
log = str[i];
if (log == ' ')
Console.WriteLine(log);
continue;
switch(colors[colorIndex])
case "red":
currentColor = ConsoleColor.Red;
break;
case "blue":
currentColor = ConsoleColor.Blue;
break;
case "green":
currentColor = ConsoleColor.Green;
break;
case "yellow":
currentColor = ConsoleColor.Yellow;
break;
default:
currentColor = originalColor;
break;
colorIndex++;
if (colorIndex >= colors.Length)
colorIndex = 0;
Console.ForegroundColor = currentColor;
Console.WriteLine(log);
Console.ForegroundColor = originalColor;
c# performance
New contributor
$endgroup$
add a comment |
$begingroup$
I'm practicing for my Technical Interview and I'm worried my code does not meet the bar. What can I do to improve this answer?
Here is the question: https://www.careercup.com/question?id=5739126414901248
Given
colors = ["red", "blue", "green", "yellow"];
and a string
str = "Lorem ipsum dolor sit amet";
write a function that prints each letter of the string in different colors. ex. L
is red, o
is blue, r
is green, e
is yellow, m
is red, after the space, i
should be blue.
My answer:
static void TestPrintColors()
string[] colors = new string[4] "red", "blue", "green", "yellow";
string str = "Lorem ipsum dolor sit amet";
PrintColors(colors, str);
static void PrintColors(string[] colors, string str)
char log;
ConsoleColor originalColor = Console.ForegroundColor;
int colorIndex = 0;
ConsoleColor currentColor = originalColor;
for (int i = 0; i < str.Length; i++)
log = str[i];
if (log == ' ')
Console.WriteLine(log);
continue;
switch(colors[colorIndex])
case "red":
currentColor = ConsoleColor.Red;
break;
case "blue":
currentColor = ConsoleColor.Blue;
break;
case "green":
currentColor = ConsoleColor.Green;
break;
case "yellow":
currentColor = ConsoleColor.Yellow;
break;
default:
currentColor = originalColor;
break;
colorIndex++;
if (colorIndex >= colors.Length)
colorIndex = 0;
Console.ForegroundColor = currentColor;
Console.WriteLine(log);
Console.ForegroundColor = originalColor;
c# performance
New contributor
$endgroup$
$begingroup$
Have you tested the code? If you have tested the code could you please provide the code that calls this function?
$endgroup$
– pacmaninbw
37 mins ago
add a comment |
$begingroup$
I'm practicing for my Technical Interview and I'm worried my code does not meet the bar. What can I do to improve this answer?
Here is the question: https://www.careercup.com/question?id=5739126414901248
Given
colors = ["red", "blue", "green", "yellow"];
and a string
str = "Lorem ipsum dolor sit amet";
write a function that prints each letter of the string in different colors. ex. L
is red, o
is blue, r
is green, e
is yellow, m
is red, after the space, i
should be blue.
My answer:
static void TestPrintColors()
string[] colors = new string[4] "red", "blue", "green", "yellow";
string str = "Lorem ipsum dolor sit amet";
PrintColors(colors, str);
static void PrintColors(string[] colors, string str)
char log;
ConsoleColor originalColor = Console.ForegroundColor;
int colorIndex = 0;
ConsoleColor currentColor = originalColor;
for (int i = 0; i < str.Length; i++)
log = str[i];
if (log == ' ')
Console.WriteLine(log);
continue;
switch(colors[colorIndex])
case "red":
currentColor = ConsoleColor.Red;
break;
case "blue":
currentColor = ConsoleColor.Blue;
break;
case "green":
currentColor = ConsoleColor.Green;
break;
case "yellow":
currentColor = ConsoleColor.Yellow;
break;
default:
currentColor = originalColor;
break;
colorIndex++;
if (colorIndex >= colors.Length)
colorIndex = 0;
Console.ForegroundColor = currentColor;
Console.WriteLine(log);
Console.ForegroundColor = originalColor;
c# performance
New contributor
$endgroup$
I'm practicing for my Technical Interview and I'm worried my code does not meet the bar. What can I do to improve this answer?
Here is the question: https://www.careercup.com/question?id=5739126414901248
Given
colors = ["red", "blue", "green", "yellow"];
and a string
str = "Lorem ipsum dolor sit amet";
write a function that prints each letter of the string in different colors. ex. L
is red, o
is blue, r
is green, e
is yellow, m
is red, after the space, i
should be blue.
My answer:
static void TestPrintColors()
string[] colors = new string[4] "red", "blue", "green", "yellow";
string str = "Lorem ipsum dolor sit amet";
PrintColors(colors, str);
static void PrintColors(string[] colors, string str)
char log;
ConsoleColor originalColor = Console.ForegroundColor;
int colorIndex = 0;
ConsoleColor currentColor = originalColor;
for (int i = 0; i < str.Length; i++)
log = str[i];
if (log == ' ')
Console.WriteLine(log);
continue;
switch(colors[colorIndex])
case "red":
currentColor = ConsoleColor.Red;
break;
case "blue":
currentColor = ConsoleColor.Blue;
break;
case "green":
currentColor = ConsoleColor.Green;
break;
case "yellow":
currentColor = ConsoleColor.Yellow;
break;
default:
currentColor = originalColor;
break;
colorIndex++;
if (colorIndex >= colors.Length)
colorIndex = 0;
Console.ForegroundColor = currentColor;
Console.WriteLine(log);
Console.ForegroundColor = originalColor;
c# performance
c# performance
New contributor
New contributor
edited 1 hour ago
chicks
1,60321019
1,60321019
New contributor
asked 3 hours ago
Austin TaylorAustin Taylor
334
334
New contributor
New contributor
$begingroup$
Have you tested the code? If you have tested the code could you please provide the code that calls this function?
$endgroup$
– pacmaninbw
37 mins ago
add a comment |
$begingroup$
Have you tested the code? If you have tested the code could you please provide the code that calls this function?
$endgroup$
– pacmaninbw
37 mins ago
$begingroup$
Have you tested the code? If you have tested the code could you please provide the code that calls this function?
$endgroup$
– pacmaninbw
37 mins ago
$begingroup$
Have you tested the code? If you have tested the code could you please provide the code that calls this function?
$endgroup$
– pacmaninbw
37 mins ago
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
Don't worry about performance.
You're writing a handful of strings to the console, and it takes milliseconds. When you start writing tens of thousands of strings, and notice it taking seconds, then you can you start to look for optimizations. Until then, the cleanliness of your code is much more important.
Your function is too long.
This is a fifty-line function. It has the responsibilities of iterating through two lists in parallel, skipping spaces, parsing color names, setting the console color, writing to console, and resetting the console color when it's all done. That's a lot! Break it up into smaller functions.
Switch statements are ugly.
I don't mean that they are never appropriate, but ConsoleColor
is an enum
, and it's possible to parse enums (while ignoring case sensitivity). You should replace this switch statement with a function call.
Don't initialize variables until you need them.
With few exceptions, modern languages are very good about optimizing variable allocation. Putting char log = str[i]
inside the loop will not result in additional memory usage, and it will save me (a potential reviewer or maintenance programmer) from having to think about that character before or after the loop.
Other tips...
You say this is practice for an interview, so it could be a good place for you to show off your knowledge of C#. With a little trouble, you could leverage Regular Expressions and LINQ to save you from manually manipulating array indexes. With a little more trouble, you could leverage IDisposable to ensure the original ForegroundColor is restored when all is said and done.
On the other hand, you could also shoot yourself in the foot attempting to do those things. If you don't honestly have in-depth knowledge about C#, it might be best just to aim for code that is as simple as possible. I think the best way to do that is to make small functions with clear names, to show you are thinking about the maintainability and reusability of your code.
$endgroup$
$begingroup$
Nicely said! Thank you for the great response! I would like to update my code and get feedback, what is the best way to do that? Sorry I'm, new here. Thank you!
$endgroup$
– Austin Taylor
1 hour ago
1
$begingroup$
Welcome to CodeReview! The usual rule is that you may make edits to your code until the first answer is posted. At this point, if you were to edit this question my answer would look silly. Please, feel free to post a follow-up question with your updated code, maybe linking back to this one for reference.
$endgroup$
– benj2240
59 mins ago
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/3.0/"u003ecc by-sa 3.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
);
);
Austin Taylor is a new contributor. Be nice, and check out our Code of Conduct.
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%2f220371%2fprints-each-letter-of-a-string-in-different-colors-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Don't worry about performance.
You're writing a handful of strings to the console, and it takes milliseconds. When you start writing tens of thousands of strings, and notice it taking seconds, then you can you start to look for optimizations. Until then, the cleanliness of your code is much more important.
Your function is too long.
This is a fifty-line function. It has the responsibilities of iterating through two lists in parallel, skipping spaces, parsing color names, setting the console color, writing to console, and resetting the console color when it's all done. That's a lot! Break it up into smaller functions.
Switch statements are ugly.
I don't mean that they are never appropriate, but ConsoleColor
is an enum
, and it's possible to parse enums (while ignoring case sensitivity). You should replace this switch statement with a function call.
Don't initialize variables until you need them.
With few exceptions, modern languages are very good about optimizing variable allocation. Putting char log = str[i]
inside the loop will not result in additional memory usage, and it will save me (a potential reviewer or maintenance programmer) from having to think about that character before or after the loop.
Other tips...
You say this is practice for an interview, so it could be a good place for you to show off your knowledge of C#. With a little trouble, you could leverage Regular Expressions and LINQ to save you from manually manipulating array indexes. With a little more trouble, you could leverage IDisposable to ensure the original ForegroundColor is restored when all is said and done.
On the other hand, you could also shoot yourself in the foot attempting to do those things. If you don't honestly have in-depth knowledge about C#, it might be best just to aim for code that is as simple as possible. I think the best way to do that is to make small functions with clear names, to show you are thinking about the maintainability and reusability of your code.
$endgroup$
$begingroup$
Nicely said! Thank you for the great response! I would like to update my code and get feedback, what is the best way to do that? Sorry I'm, new here. Thank you!
$endgroup$
– Austin Taylor
1 hour ago
1
$begingroup$
Welcome to CodeReview! The usual rule is that you may make edits to your code until the first answer is posted. At this point, if you were to edit this question my answer would look silly. Please, feel free to post a follow-up question with your updated code, maybe linking back to this one for reference.
$endgroup$
– benj2240
59 mins ago
add a comment |
$begingroup$
Don't worry about performance.
You're writing a handful of strings to the console, and it takes milliseconds. When you start writing tens of thousands of strings, and notice it taking seconds, then you can you start to look for optimizations. Until then, the cleanliness of your code is much more important.
Your function is too long.
This is a fifty-line function. It has the responsibilities of iterating through two lists in parallel, skipping spaces, parsing color names, setting the console color, writing to console, and resetting the console color when it's all done. That's a lot! Break it up into smaller functions.
Switch statements are ugly.
I don't mean that they are never appropriate, but ConsoleColor
is an enum
, and it's possible to parse enums (while ignoring case sensitivity). You should replace this switch statement with a function call.
Don't initialize variables until you need them.
With few exceptions, modern languages are very good about optimizing variable allocation. Putting char log = str[i]
inside the loop will not result in additional memory usage, and it will save me (a potential reviewer or maintenance programmer) from having to think about that character before or after the loop.
Other tips...
You say this is practice for an interview, so it could be a good place for you to show off your knowledge of C#. With a little trouble, you could leverage Regular Expressions and LINQ to save you from manually manipulating array indexes. With a little more trouble, you could leverage IDisposable to ensure the original ForegroundColor is restored when all is said and done.
On the other hand, you could also shoot yourself in the foot attempting to do those things. If you don't honestly have in-depth knowledge about C#, it might be best just to aim for code that is as simple as possible. I think the best way to do that is to make small functions with clear names, to show you are thinking about the maintainability and reusability of your code.
$endgroup$
$begingroup$
Nicely said! Thank you for the great response! I would like to update my code and get feedback, what is the best way to do that? Sorry I'm, new here. Thank you!
$endgroup$
– Austin Taylor
1 hour ago
1
$begingroup$
Welcome to CodeReview! The usual rule is that you may make edits to your code until the first answer is posted. At this point, if you were to edit this question my answer would look silly. Please, feel free to post a follow-up question with your updated code, maybe linking back to this one for reference.
$endgroup$
– benj2240
59 mins ago
add a comment |
$begingroup$
Don't worry about performance.
You're writing a handful of strings to the console, and it takes milliseconds. When you start writing tens of thousands of strings, and notice it taking seconds, then you can you start to look for optimizations. Until then, the cleanliness of your code is much more important.
Your function is too long.
This is a fifty-line function. It has the responsibilities of iterating through two lists in parallel, skipping spaces, parsing color names, setting the console color, writing to console, and resetting the console color when it's all done. That's a lot! Break it up into smaller functions.
Switch statements are ugly.
I don't mean that they are never appropriate, but ConsoleColor
is an enum
, and it's possible to parse enums (while ignoring case sensitivity). You should replace this switch statement with a function call.
Don't initialize variables until you need them.
With few exceptions, modern languages are very good about optimizing variable allocation. Putting char log = str[i]
inside the loop will not result in additional memory usage, and it will save me (a potential reviewer or maintenance programmer) from having to think about that character before or after the loop.
Other tips...
You say this is practice for an interview, so it could be a good place for you to show off your knowledge of C#. With a little trouble, you could leverage Regular Expressions and LINQ to save you from manually manipulating array indexes. With a little more trouble, you could leverage IDisposable to ensure the original ForegroundColor is restored when all is said and done.
On the other hand, you could also shoot yourself in the foot attempting to do those things. If you don't honestly have in-depth knowledge about C#, it might be best just to aim for code that is as simple as possible. I think the best way to do that is to make small functions with clear names, to show you are thinking about the maintainability and reusability of your code.
$endgroup$
Don't worry about performance.
You're writing a handful of strings to the console, and it takes milliseconds. When you start writing tens of thousands of strings, and notice it taking seconds, then you can you start to look for optimizations. Until then, the cleanliness of your code is much more important.
Your function is too long.
This is a fifty-line function. It has the responsibilities of iterating through two lists in parallel, skipping spaces, parsing color names, setting the console color, writing to console, and resetting the console color when it's all done. That's a lot! Break it up into smaller functions.
Switch statements are ugly.
I don't mean that they are never appropriate, but ConsoleColor
is an enum
, and it's possible to parse enums (while ignoring case sensitivity). You should replace this switch statement with a function call.
Don't initialize variables until you need them.
With few exceptions, modern languages are very good about optimizing variable allocation. Putting char log = str[i]
inside the loop will not result in additional memory usage, and it will save me (a potential reviewer or maintenance programmer) from having to think about that character before or after the loop.
Other tips...
You say this is practice for an interview, so it could be a good place for you to show off your knowledge of C#. With a little trouble, you could leverage Regular Expressions and LINQ to save you from manually manipulating array indexes. With a little more trouble, you could leverage IDisposable to ensure the original ForegroundColor is restored when all is said and done.
On the other hand, you could also shoot yourself in the foot attempting to do those things. If you don't honestly have in-depth knowledge about C#, it might be best just to aim for code that is as simple as possible. I think the best way to do that is to make small functions with clear names, to show you are thinking about the maintainability and reusability of your code.
answered 2 hours ago
benj2240benj2240
78618
78618
$begingroup$
Nicely said! Thank you for the great response! I would like to update my code and get feedback, what is the best way to do that? Sorry I'm, new here. Thank you!
$endgroup$
– Austin Taylor
1 hour ago
1
$begingroup$
Welcome to CodeReview! The usual rule is that you may make edits to your code until the first answer is posted. At this point, if you were to edit this question my answer would look silly. Please, feel free to post a follow-up question with your updated code, maybe linking back to this one for reference.
$endgroup$
– benj2240
59 mins ago
add a comment |
$begingroup$
Nicely said! Thank you for the great response! I would like to update my code and get feedback, what is the best way to do that? Sorry I'm, new here. Thank you!
$endgroup$
– Austin Taylor
1 hour ago
1
$begingroup$
Welcome to CodeReview! The usual rule is that you may make edits to your code until the first answer is posted. At this point, if you were to edit this question my answer would look silly. Please, feel free to post a follow-up question with your updated code, maybe linking back to this one for reference.
$endgroup$
– benj2240
59 mins ago
$begingroup$
Nicely said! Thank you for the great response! I would like to update my code and get feedback, what is the best way to do that? Sorry I'm, new here. Thank you!
$endgroup$
– Austin Taylor
1 hour ago
$begingroup$
Nicely said! Thank you for the great response! I would like to update my code and get feedback, what is the best way to do that? Sorry I'm, new here. Thank you!
$endgroup$
– Austin Taylor
1 hour ago
1
1
$begingroup$
Welcome to CodeReview! The usual rule is that you may make edits to your code until the first answer is posted. At this point, if you were to edit this question my answer would look silly. Please, feel free to post a follow-up question with your updated code, maybe linking back to this one for reference.
$endgroup$
– benj2240
59 mins ago
$begingroup$
Welcome to CodeReview! The usual rule is that you may make edits to your code until the first answer is posted. At this point, if you were to edit this question my answer would look silly. Please, feel free to post a follow-up question with your updated code, maybe linking back to this one for reference.
$endgroup$
– benj2240
59 mins ago
add a comment |
Austin Taylor is a new contributor. Be nice, and check out our Code of Conduct.
Austin Taylor is a new contributor. Be nice, and check out our Code of Conduct.
Austin Taylor is a new contributor. Be nice, and check out our Code of Conduct.
Austin Taylor is a new contributor. Be nice, and check out our Code of Conduct.
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%2f220371%2fprints-each-letter-of-a-string-in-different-colors-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
$begingroup$
Have you tested the code? If you have tested the code could you please provide the code that calls this function?
$endgroup$
– pacmaninbw
37 mins ago