Is this you? As a journalist, you can create a free Muck Rack account to customize your profile, list your contact preferences, and upload a portfolio of your best work.
Claim your profile
Articles by Simon André Forsberg
Oh my goodness: How many selfies have I taken?
The title is a joke on how some people take a lot of selifes pictures Recently, in The 2nd Monitor, there was a brief discussion on selfie answers, starting with this post. At one point, a user said this: I immediately thought: this is the perfect opportunity for an SEDE query! Try it out! The above query takes the user's ID and returns a list of all the selfie answers that they have posted.
Numbers, pairs, and a difference
The basic algorithm you use is logical, and well presented. The general style is standard, and I see no issues there. I believe the algorithm can be changed slightly to make the comparison code simpler, but the performance will still be comparable. I looked for edge cases I would have expected, specifically relating to large differences, but you appear to have covered that well. Unfortunately, your pair-counts for are all off. For example, your third test case: should have a count of 6, and not 18.
Rock, Paper, Scissors, Lizard and Spock
One think that I quickly noticed is the ammount of repetitions. But one that highly ticked me of is the ammount of tries. You could use an early return, like this: There, we cleaned up the ties! You have misspelled twice. For your code to work, you have to know what's what.
What's the difference between odd and even numbers?
Instead of counting two sums (of odd and even digits) and returning their difference, you could use a single sum value, subtracting a digit's value if it's even and adding if it's odd. An added benefit of this approach is that you are more protected from integer overflows: if odd and even digits are interleaved, the two-sum approach will be much less likely to overflow.
Character instance slowdown
The most important difference between your and your is this: The rest can work just fine without being in if or else. Also, when you copy-paste code, which you apparently did to check the bounds on , you should change the comments. Both of your comments say "go below zero". It was a bit hard for me to understand the purpose of your .
Desktop Notifications for flags and queue items
You did a really good job at getting rid of most magic numbers/values at the top of your code. However, there is still this line: Where did the 60 comes from? And the 1000? You could declare these as constants, and then use the constants to create . Other than that, very nice job here. You call the function a few times in your code. Like here: Although it is not commonly known, the function actually takes a second parameter called the .
String related problem from CodingBat
I was trying this problem and I was wondering if there is a way to check for the edge cases inside the for-loop. We'll say that a lowercase 'g' in a string is "happy" if there is another 'g' immediately to its left or right. Return true if all the g's in the given string are happy. Here I am starting the loop from index 1 as I need to look back and I don't want an exception. The same applies for the end edge case where I need to look the following index.
Please Save My Name
I wanted to create a save game system for my city building game that did not require the player to input a name for the city. It was also important to allow infinite save games, so I could not just use save slots to solve the problem. With a bit of help, I devised a system that would allow the player to optionally enter a name, and if they didn't the game would determine a default name based on the existing save names.
50 State Info Program
In , the variable is unused. The method iterates over all entries in , even after it found a match. It could return immediately, and without using the local variable: The other inefficiency is that looking up a state by iterating over a list is an \$O(N)\$ operation, and you can do much better by building a , turning it into an \$O(1)\$ operation, which is especially important if you intend to do this repeatedly. Exiting in the middle of the program with is not pretty.
50 states info program
In , the variable is unused. The method iterates over all entries in , even after it found a match. It could return immediately, and without using the local variable: The other inefficiency is that looking up a state by iterating over a list: for example to find "Wyoming", the program has to step through all previous elements. If you use a instead of an array, then lookups can be instantaneous or close to that. Exiting in the middle of the program with is not pretty.
Guess the randomly generated number game
I've been working on this for the last few days. Any improvement ideas? (Notes are very simple, making this for school and notes have to be for complete novices to Python). import random # Allows the computer to generate random numbers, vital for the program to function correctly. import sys # Allows a new selection of system related commands to be used; I use sys.exit() to close the program. def oneToTen(): ten = random.randint(1,10) #Generates a random integer between 1 and 10.
Equals method for a class representing an origin and destination
You have a bit of repetition between your methods. Since no instance is equal to , you can replace your entire overridden method with: If the is not an , this will just pass in a which will then return false. At a pinch, XOR is a passable way of combining hash-codes. But it has collision issues: So of those it's likely to be the second, if any, which is of concern in this particular case.
Shielding your CodeReview on Github
In response to a feature-request on meta, I spent a few hours today creating the feature in php. This adds a 'review shield' to your GitHub repository (or wherever else you want it). It looks like the following: To use it, use something like this: Replace with the question id you want to direct to, and replace with your user id (so that you can get the Announcer / Booster / Publicist badges). I used shields.io to get the basic SVG XML and adapted that.
Is there a design pattern or a better way to refactoring this?
The 's responsibility is cache any kind of object. For that reason, I have the method's param (I need to find a better name). is for those objects which need a param for its creation. For example: As you can see, needs a param (sourceCode) for its creation. In some part of the code, I fill the ... In the case of the : Here is my problem. As you can see, I create the object with the first element of array (remember the in the above method's signature).
A generic cache object
The 's responsibility is cache any kind of object. For that reason, I have the method's param (I need to find a better name). is for those objects which need a param for its creation. For example: As you can see, needs a param (sourceCode) for its creation. In some part of the code, I fill the ... In the case of the : Here is my problem. As you can see, I create the object with the first element of array (remember the in the above method's signature).
Populating a bag of Scrabble tiles
I'm sorry, I just love to make things flexible so much that I have to take it one step further... I will use a mix between @EthanBierlein's version and your original, and improve on that, making the following changes: There is really no reason in my opinion to separate the configuration of how many tiles of a certain letter there should be, and how many points that tile should be worth.
Finding the percentage of even numbers in an array
Potential bug: What if the array contains a single element, and it is even? if(a.length <= 1) { return 0.0; } That's what happens. Easy fix, change to: if(a.length == 0) { Your indentation is slightly screwed up, but that might just be a copy-paste error. double even should be int even. You could use a for-each loop, for (int i : a) { if(i % 2 == 0) { int[] a could be named int[] array or int[] values or int[] input.
How many albums and songs do we have? Returning a string representation using StringBuilder
What this method should do is return a String that displays how many albums and songs there are. This method is a private method of my Artist class which has a method called and . The whole class probably needs an improvement but I will try that later, at first I'd like this single method to be improved if possible. I used the class because I heard a dozen of times that it will improve performance when concatenating Strings.
Returning a string representation of the number of albums and songs available
What this method should do is return a that displays how many albums and songs there are. This method is a method of my class which has a method called and . The whole class probably needs an improvement but I will try that later. First I'd like this single method to be improved if possible. I used the class because I heard a dozen of times that it will improve performance when concatenating Strings. I wasn't sure about those two lines : Are they good enough with the comments above them?
A (late) Simple Calculator
As I was looking through my past questions, I noticed my really old calculator question (here and here). Considering it looked like a huge mess, I decided to rewrite it. Coincidentally, the April 2015 Community Challenge was to implement a simple calculator. It might be a bit late, but here it is: The logic here is fairly simple: In addition (or subtraction or multiplication or whatever), you go from left to right: is essentially the same as: but with , you have to go from right to left.
Classes for Questions and Answers
About your primary question: I'd definitely go with whenever I have the possibility to do so. As you can pass on an array to a varargs parameter, but not varargs to an array parameter, varargs is the better option IMO. It is convention to not prefix boolean fields with .
Deciding between four user statuses based on two flags
I think it is time to bring out the ! Enums in Java are pretty handy overall when there is a fixed number of possible values. In this case there is only four possible values, so an enum can be a good candidate for the job. You can get the string representation of the enum by calling or Overall I don't recommend using Strings for this kind of data, you have no guarantee that your will only have one of the four possible values. What if the String suddenly says "ACCPT_SMTHNG"?
Student Showdown - Q and A battle game, part 1: The Champions
Just a self challenge/fun thing. I have a scene where the player picks a character, of course it works but I'm repeating myself an awful lot. I would appreciate any formatting suggestions and making it more streamlined in general.
Student Showdown - Q and A battle game, part 1
Just a self challenge/fun thing. I have a scene where the player picks a character, of course it works but I'm repeating myself an awful lot. I would appreciate any formatting suggestions and making it more streamlined in general.
Bubble sort algorithm in JavaScript
Let me start with the interesting question: Is it even a bubble sort? This sort is a bit like bubble sort and a bit like insertion sort. You have re-invented gnome sort, so congratulations, that's a nice coup for a beginner. The other answers -- which state that you should not access arrays out-of-bounds, and should use to declare local variables rather than relying on automatic declaration to save you, are correct.
Locker Puzzle in Java
The author of my book does not release odd-numbered solutions to students, so I can't check my solutions. I was wondering if there was a better way to solve this problem without simply printing out the square roots. Here is the question: (Game: locker puzzle) A school has 100 lockers and 100 students. All lockers are closed on the first day of school. As the students enter, the first student, denoted S1, opens every locker.
Optimizing Manhattan-distance method for N-by-N puzzles
EDIT: By N-by-N puzzles I mean f.e. a 3x3 sliding puzzle with one blank space (so a 8-puzzle) or a 4x4 sliding puzzle (so a 15-puzzle). When I try to solve certain puzzles some take no time but others take to much time to the point that I run out of memory. I think it's due to how I calculate the manhattan distance. Below you see the , an instance of this class holds a puzzle in the variable . The methods and is used to get the the row and column of a certain value.
Given a sequence of positive integers A and an integer T, return true if a continuous sequence of A sums up to exactly T
It is a pleasure to tell you this: Excellent! Your approach is precisely what I had in mind when I read the challenge description (before looking at your code). Excellent that you have unit tests! Definitely helps in checking if it's possible to clean up your method. My only suggestion would be to make them Parameterized, which should be a lot more helpful if one or more of them fails.
Cutting The Sticks
You are given N sticks, where each stick has the length of a positive integer. A cut operation is performed on the sticks such that all of them are reduced by the length of the smallest stick. Suppose we have six sticks of the following lengths: Then, in one cut operation we make a cut of length 2 from each of the six sticks. For the next cut operation four sticks are left (of non-zero length), whose lengths are the following: The above step is repeated until no sticks are left.
When something that was supposed to be fun became not fun
StackEgg! What a great invention. Probably the best thing to hit Stack Exchange since the invention of unicoins! Very entertaining game, very fun to play. Many users have probably spent several hours playing this. And April Fool's day is not over yet. However, where there is great inventions there seems to be users who use things for evil. Some users from Programming Puzzles and Code Golf have apparently raided other sites' stack eggs, forcing them to restart.
Show More
loading
Actions
Is this you?
As a journalist, you can create a free Muck Rack account to customize your profile, list your contact preferences, and upload a portfolio of your best work.Get in touch with Simon André
Contact Simon André, search articles and posts on X, monitor coverage, and track replies from one place.
Learn more about Muck Rack