Things I can think of things like integer overflow, if the input type is int etc. But other than that, what do you think is wrong with this code, design wise, style wise , also in terms of other edge/corner cases.
binaryheap.h
#ifndef INCLUDE_BINARYHEAP_H_
#define INCLUDE_BINARYHEAP_H_
#include
The following is an TaskScheduler that always run tasks in a thread it maintains.
When created, a name of the thread was specified. Once you schedule the first task, untill it is been Disposeed, a thread will be created and wait for tasks to execute.
The reason of this class is that sometimes th...
I am working on a program in which 10000 random no repeat numbers are selectively sorted into ascending order.
I have written the following code:
import java.util.Random;
public class Sorting {
public static void main (String[] args){
//Here, we initiate an array with the integers...
"Selection sort" as a title would've been even more generic. Unfortunately, we'll naturally get a lot of the same types of programs. Eventually, we'll have titles such as "Linked list implementation #455 in C++"
I am working through Kernighan and Ritchie to brush up on my C skills, so I thought I would invite critiques of my code.
Here is my Hex-to-int function:
int htoi(char *str, unsigned long long *result)
{
int c;
int i;
int msnibble;
int digits;
const int nibbles[] = {'\x0','\x...
It also appears he has added more code to the second code block, so the reviews may be hard to follow. Other than that, I don't think he edited the original code.
I build phone apps and I use JSON a lot in my APIs.
The problem is sometimes I don´t know how how to format the JSON data.
For example iOS UITableView takes NSArray as datasource.
{
"contact_id": [
"455",
"464"
],
"contact_name": [
"Administrator",
"...
@Jamal... can I ask you to intercede on this one... I don't want to be the one:
@rolfl: I just updated the question.. I miss couple of important points.. I have rewrote most of the things.. Now take a look.. It will make sense now.. I guess now you will be angry as in the question lot of things might have change as corresponding to what you have suggested :( — user280956452 secs ago
If the update was only a clarification and had nothing to do with the suggestions in your answer, then you may have to update yours based on the changes.
In terms of invalidations, this is one of the lesser likely cases.
I am following up on my previous question here
Problem Statement:-
I was ask this interview question recently.. I was able to come up with the below code only which runs in O(k log n)-
Given k <= n sorted arrays each of size n, there exists a data structure requiring O(kn) preprocessing time ...
I'm working on extending the functionality of a photo gallery by adding a thumbnail display, I have multiple if statements set up to adjust the position of the thumbnails. Obviously this is not ideal, I'm trying to put my head around how to use a for loop to iterate the width multiplier and curSl...
guys in the replace multiple if statements with foor loop question, the selected answer has this piece of code "Math.floor(((currSlide || 1)-1)" what does the or operator do? its not inside a condition
I've written a function that works fine, but being new to jQuery, I'd like to know if there's a more cleaner way of writing it. Anything advice would help; just trying to learn!
function displayContent() {
var $link1 = $('.row.nav li a.bio');
var $link2 = $('.row.nav li a.stylist');
var $l...
function displayContent() {
// put all links together
var $links = $('.row.nav li a').filter(".bio,.stylist,.contact");
var $content = $('#text-content')
var $bio = $("#bio");
// put these together since they're used in the same way
var $stylist_contact = $("#stylist,#contact");
...
I am using Netty embedded within Grails to process and display incoming SNMP messages. Since Netty 4 doesn't come with a built-in ChannelExecutionHandler I had to make my own and would like some feedback on it.
I have created a Gist as there is a decent amount of code and separate files to look ...
The following shows that "0" is false in Javascript:
>>> "0" == false
true
>>> false == "0"
true
So why does the following print "ha"?
>>> if ("0") console.log("ha")
ha
The following Java code reads lines from an input text file and will output the entry that has the highest number in field-2 to an output file.
INPUT:
William J. Clinton 6 Q1124
42nd President of the United States 6 Q1124
Bill Clinton,6,Q1124
Bill Clinton,14,Q9890
Bill Clinton,5,Q9880
Will...
I'm far from a Java expert, but a few things jumped out at me
You forgot to close the reader (br).
Do not close the streams inside of the try. What happens if an exception gets thrown before the close commands are reached? You need a finally block. Or, better yet, if you're using Java 7 or ...
Just a few things that stick out to me:
What is cons? Don't you mean Node? At first I thought it was just a short form of constructor, but it appears to be corresponding to a node.
I'd strongly recommend using a different variable name than new. Although this is C and not C++, this could sti...
Original: http://www.php.net/manual/en/function.crypt.php#114060
I've removed the original author's comments to give you a quick glance at the code. Visit the link for detailed explanations of each method's use.
I've tested it with success and since I am brand new to the world of security and...
call from my index.php to function get_data($curr_date) goes from here
case "events" :
$curr_date = $_POST['current_date'];
if ( $eid = get_data($curr_date) )
{
$result = array( "success"=>true,
...
I have a ruby code segment which I need to DRY up. Following is the code segment.
def disable_attribute
statement1
@response = Client::Service.disable_attribute(
param1: param1value,
param2: param2value
)
statement2
end
def enable_attribute
statement1
@response = Client::...
I wrote this function as part of interview practice. This method removes a character from a given string.
I was wondering how I could make this code more efficient when it comes to runtime/space. I think my code is O(n) and I'm not sure if I can increase the efficiency. However, perhaps using th...
i'm a c++ newbie, this is an algorithm i'm trying to optimize.
i was trying to use openMP without any success.
please try to explain why do i need to change any line of code. I want to learn.
also i wish to understand is using openMP is the best way for matrix based algorithms?
Thanks
void CAlg...
Newbie here. Please bear with me. so I did a php mailer script. It does the basic validation of fields, return errors, else submit if all is good. But it also has a honeypot field that is not required to be filled in (I'm assuming by hiding it using css a spambot will fill in the field anyway). i...
import java.util.*;
import java.io.*;
public class Player implements Comparable<Player>
{
Scanner read;
PrintWriter write;
private String playerid;
private int playerscore;
int x = 0;
ArrayList<String> n = new ArrayList<String>();
ArrayList<I...
Need help about my examinable command pattern, it just becomes too complicated to continue: What I need is a examinable comamnd, so I could examine then execute something of cleaning. here is what is done:
public interface ICommand {
public void execute();
}
public interface IExaminable {
...
@Vogel612 Not entirely, useless code is more than just randomly named never used variables. if (!this.Value.equals("")) is not a random unused variable. It is however, completely useless code.
I have the following sql code. It is working fine and the structure seems to be correct. Though it outputs something, but not correct. what could be the problem? Is it a mysql drawback?
Select regd, Section, Test_date,
SUM(IF(t_scored IS NULL,0,t_scored)+IF(w_scored IS NULL,0,w_scored)+IF(f_scor...
I have built the following code to download stock data from yahoo finance. The plan is to then use the inbuilt pandas functions to calculate metrics from this data that will then be used as features for classification. I also try to calculate whether or not the asset performed better or worse or ...
$filterType = array( // set up an array that will hold specific flags needed for PHP's filter_var() function.
'string' => FILTER_SANITIZE_STRING, //Strip tags, optionally strip or encode special characters
'integer' => FILTER_VALIDATE_INT, //Validate value as integer, optionally from the speci...
I have an issue with the following code, which I isolated to the most closed form, I am using Java 8, which is almost ready for launch, so I expect no serious issues in the implementation itself, so it may/must be my own code:
public abstract class Drawable implements DrawableInterface {
}
...
I define some variables needed in my app with a enum
but this values are not compared in a switch,
Is it a bad practice to use this variables not in an enumeration?
should they just be declared as int?
I've refactored the string calculator kata as much as I could and tried to maintain single responsibility. Is there anything I could factor differently? The specs can be reviewed here: http://osherove.com/tdd-kata-1/
module StringCalculator
def self.add(string)
string.end_with?("\n") ? fai...
I'm trying to reduce the complexity of some methods, and I'm not exactly sure what approach to take. I'm currently building a PHP wrapper for a REST API, and the main problematic class is here:
https://github.com/petrepatrasc/blizzard-starcraft-api/blob/master/Service/ApiService.php
In short, s...
You can get rid of the <type_traits> header but replacing your std::enable_if by a bool template specialization:
template <::std::size_t, ::std::size_t, bool>
struct expand_indices_impl;
template <::std::size_t A, ::std::size_t B>
struct expand_indices:
expand_indices_impl<A, B, A==B>
{};
...
I have some PHP code I'm working on which contains 2 queries and I need to set extra conditions for 2 out of 3 view options and one less query for 1 out of 3 view options.
Basically I'm rendering a history list of shows with three options ALL | PRO | UPCOMMING. The ALL option should include both...
Things that I'm not sure about:
Whether this works in all use cases - alongside routing and within templates etc
Am I polluting the scope with all these variables? This seems to be the easiest way to do unit tests - or should I be passing the vars into the function? Some ( documentHeight, USerS...
For several months I'm working with JavaScript and THREE.JS library. Firstly I dislike the prototype-based programming, because I have to work a lot of with class-oriented languages, where any class is represented as the type and there is a strict separation between declaration and behavior of pr...
Is there a better way to do this kind of array combination like a php native function like array_combine, I'm trying to improve my skills, and in certain point I note that I using too much nested foreach for almost everything.
Using PHP 5.4
$array = Array(
'GroupName' => 'Emilio',
...
In reviewing code in our application, I see a tension between what makes a convenient design for methods accessing database data versus coding to ensure that the JDBC resource objects are closed properly.
An example problem:
// Application.java: Singleton class for objects which want to execute...
I have a remote repository on GitHub with two branches for my website. A master and a dev branch. Locally, I have a dev repository and a master repository.
Problem: Although using git commit -am "message" and git push origin dev (or git push origin master respectively) works for both repositorie...
I have the following key value pair in the array and trying to extract and load in the collections
the below code is working but i'm thinking if i have any room to optimized this code using linq?
string _data = "Website=url:www.site1.com,isdefault:true,url:www.site2.com,isdefault:true";
List<W...
I wrote a program for one of my Object Oriented Programming in Java classes(Program 2) and I got docked off because my method was too long, which I'm not complaining about, but I got the note: Modularize more, method too long SO: I was wondering what any of you more experienced programmers would ...
I'm trying to write a code to monitor a 'starsvalue' on a webpage (localhost:9911/star?request=1) every 5 second and when the 'starsvalue' changes to go this link ( localhost:9911/star?transf=2&stars='starsvalue') with 'starsvalue'/100-1.
When I go to this link 'localhost:9911/star?request=1' I s...
I wrote this java program for a tutorial of my 1st year java module. And I posted it as a blog post and in Github.
I validate it for
Non numeric inputs
x/0 type fractions
0/0 and 0/y type fractions
x/1 and x/x type fractions
-x/-y and x/-y type fractions
appreciate if someone can look in to ...
So this is the requirement I have ( from the book: Cracking the Coding Interview )
And this is my implementation:
public interface CallAllocator {
public Employee getCallHandler() throws NoEmployeeInTheHouseException;
void setTL(TechnicalLead technicalLead);
void setPM(ProductMana...
Some time ago I made my simple game loop, so here's the code:
public class Game {
private final int TARGET_FPS = 60;
/** optimal waiting time in milliseconds */
private final long OPTIMAL_TIME = 1000 / TARGET_FPS;
/** last Frame time */
private long lastFrame;
private i...
I wrote this java program for a tutorial of my 1st year java module. And I posted it as a blog post and in Github.
I validate it for
Non numeric inputs
x/0 type fractions
0/0 and 0/y type fractions
x/1 and x/x type fractions
-x/-y and x/-y type fractions
appreciate if someone can look in to ...
Your mailer is vulnerable to a header-splitting attack! (Also known as a CRLF-injection attack.)
Basically, if one of the fields contains a CRLF, the attacker can insert any arbitrary header into the generated message. For example, if the email parameter is
victim1@example.com%0d%0aCc:%20vict...
@200_success: Based on yours and William Morris' answer on the hex-to-int question, I'm not sure if I should keep my answer, unless mine could be considered an alternative in some situations. Then again, I never really read what that C function actually does.
@Jamal Now that the OP has posted revised code based on your advice, your answer now has historical significance. You should leave it up because even unpopular advice has educational value. Hopefully your answer will get outvoted.
I think you should be returning different error values for different errors. You're returning -1 for an invalid pointer, too many hex digits, no hex digits, and a non-hex character detected.
Instead, choose separate error values for each of these (and any additional error types) and return the ...
That is true, and I am okay with out-voting. I have upvoted yours and William's already, of course. Since this was a hot question for a bit, I'm surprised some SO people didn't leave comments.
I've been considering the pro's and con's between implementing my page objects with privately backed properties and lazily instantiating them or just returning new instances every time and am curious what other people think is the best practice.
For example, I could have something like;
pri...
Is a CR question with "non-compilable" code allowed? I mean I am making a 3D game which has now already a quite large sourcecode, while the question is only about part of it
@skiwi Your whole program must work. However, you can provide just the relevant excerpt in the question to be reviewed, which obviously won't compile standalone.
There are several things that can be improved in your code, I will point out some of them here.
Polymorphism
Your Addition/Subtraction/Multiplication/Division classes has a lot in common. You should use polymorphism and inheritance to use them better. You can make an abstract class for them.
A...
I wrote this function as part of interview practice. This method removes a character from a given string.
I was wondering how I could make this code more efficient when it comes to runtime/space. I think my code is O(n) and I'm not sure if I can increase the efficiency. However, perhaps using th...