New answers tagged

3

Updating my comments on current implementation, Thanks @Mast for pointing out. You have used let for output, targetIndex and title. None of them never updated. const is better suitable here. Usage of value.title.indexOf('_') is repeated. you can use variable. You can avoid substring usage because you are already did value.title.split('_') and you can use ...


1

Consider absl::Span<T>. (or boost's) It may one day be replaced with std::span It's actually quite hard to make what you're asking for, despite this being extremely useful and desired for a very long time. However, with it, you can write: void ReadFromIntArray(absl::Span<int const> int_array); ReadFromIntArray({1,2,3}); ReadFromIntArray(std::...


2

Your code is pretty sound. I can't see any improvements that specifically use ES5/ES6 tricks - the spread operator for example will set all of the other keys to undefined instead of leaving them empty, which I think is undesired. Further loops would iterate over those undefined keys as well. That said, here are some improvements: Use reduce to remove the ...


4

Is inheritance really the answer? I think it's been covered that you could dynamically build your product instances, but it's not really clear to me why you'd need to. The object model feels a bit wrong to me. From what I can see you're reading all of the objects in, then storing them in an ArrayList<Product>. There doesn't appear to be a lot of ...


13

First, a Code Review Anti-pattern Please don't do this: record = new String(); This is (barring compiler heroics), creating a brand new empty string literal, when a perfectly good interned string literal is available: record = ""; Using this interned literal "" prevents the creation of a number of identical tiny objects, and will improve the ...


3

I think there are two choices with little or no in-betweens: formally specify the products and use a parser keep the current behavior, which means determining the product and switching. so in the end, you have to either take a big step and create a formal description and a parser (parser generation is a university course, mind you). Or you'll have to ...


1

I restructured the code into object oriented format. Incorporated changes suggested by pacmaninbw. #include <iostream> #include <vector> class replace_avg{ private: int n; int tmpIn; double f; double l; double av; std::vector<double> intputData; public: std::vector<double>...


1

Always check input succeeded. std::cin >> n; std::cin >> arr[i]; In both these lines, we ignore all errors, and will produce the wrong output without any warnings, and happily return 0 from main(). This is bad for any program used as a processing step (e.g. driven by Make).


2

Something to keep in mind is that this statement is truncating data: arr[j]=av; because it is assigning a double to an int. As was mentioned in a comment there are c++ container types that would be better than a old C style array, two of these are std::array<type, arraySize> and std::vector<type>. std::array is a fixed size and std::...


1

From the advise of MarkM. Finding the min of absolute values of the array. A bit more readable than his approach (personal opinion). function closestToZero(arr) { if (!arr || arr.length === 0) { return 0; } let closestToZero = arr[0]; arr.forEach(function(number) { if (Math.abs(number) < Math.abs(closestToZero)) { ...


1

Don't using namespace std; - especially not in a header, where it inflicts the harm on every source that includes the header. Prefer nullptr to NULL, because the former is more strongly typed. Use std::size_t for indexing, rather than int. When overloading operator[], it's usually necessary to provide two versions: int& operator[](std::size_t); int ...


0

I would like to point out few things in your code:- This should be at the first place of your function. if(typeof(arr) === 'undefined' || arr.length === 0) { return 0 } You have used arrow function here which is of no use here, as according to rules if I tell you, arrow function must be used in one liner function. You could have not used anonymous ...


2

This isn't really a code review so much as help on how to get this to compile. Array::Array(int *p = NULL, int s = 0){ default parameters go on the declaration, not the definition.* Array::Array& operator=(Array other){ Qualify the operator with Array::, not the return type. You're not returning an Array::Array**, you're defining the operator= member ...


3

A short review; You probably want to cache this.content.length That check for the end of array is clunky separator will change from 0 to "", an unlikely edge case, but it could happen Why are array elements in this.content ? I would have gone for this[i] instead string is not very evocative, I went with out Obligatory rewrite; Array.prototype.myJoin = ...


4

first, using Parse for user input it would be a bad practice, as you're parsing the input without any validation. thus, you'll need to use TryParse instead, which will validate the input to the specific type, if it's valid input that would be acceptable by the targeted type, then it would return true and output the parsed value. Example direct parsing : //...


0

Your formatting is bad, adhere to PEP8 for readability. j is always n * 24, so you could eliminate one of them. I'd probably use j exclusively. Instead of manually iterating an integer in a while loop, use range(). In particular, check out the third argument to it. Since you know the final size of ll, you could allocate the required amount of storage up ...


4

Your reviewers are correct. You are doing way too much work here. To find the number closest to zero you only need to find the smallest absolute value in the list. You should also clarify in the interview if the list can contain non-numeric values. There's no reason to test that the values are numbers unless you need to for the purpose of the review, ...


0

Consider using Set for filteredFiles. I know it is a small array to iterates through for calls to Array.includes(), but it is just general good practice IMO to use an appropriate data structure for a key lookup type use case. asynchronous functions are not supported inside map() though in this case it doesn’t really matter as you are just mapping an array ...


3

variable length arrays are required in C99 and optional in C11, C17/18. To detect, something like #if defined(__STDC__) && defined(__STDC_VERSION__) && \ (__STDC_VERSION__ == 199901 || (__STDC_VERSION__ >= 201112 && __STDC_NO_VLA__ != 1)) #define VLA_OK 1 #else #define VLA_OK 0 #ednif If code does not use a variable ...


-2

import math import os import random import re import sys # Complete the hourglassSum function below. def hourglassSum(arr): large=-64 for i in range(4): for l in range(4): sum=0 for j in range(i,i+3): for k in range(l,l+3): if (j==i+1 and k==l)or(j==i+1 and k==l+2): ...


2

Some actions I go through are asynchronous, so I have to wait for them to completely finish before passing to a new one. That's right. However, your interpretation of that statement is wrong. The actions need to be serialized, but only on a per file basis. const buffer = await file.buffer(); return await this.bufferIsImageFile(buffer); Concurrency You ...


6

Type-wise, it is ok to go from void pointers to array pointers and back. As long as the "effective type" is an int array of the specified size. Void pointers do however have non-existent type safety, so they should be avoided for that reason. The best way is rather to use an array and let the compiler "adjust" it to an array pointer between the lines: void ...


4

One suggestion will be to put your parameters on a dedicated struct struct my_array { void *data; size_t rows; size_t cols; } So your functions will only have one parameter as input.


2

It may make sense to turn voucher.categories into a Set (or store them in a Set instead of an array in the first place) to speed up the contains call, however that is only sensible, if you expect it to contain a lot of items. Other than that I'd move the check for the existence of voucher.categories (and add a check if it's empty) outside the expression, in ...


3

Normally there are a lot of categories, so I'd go through the voucher.categories array: const filterCategories = (voucher, categories) => ( Array.isArray(voucher.categories) ? voucher.categories .map(categoryId => { const categoryFound = categories.find(c => c.id === categoryId); return categoryFound ? categoryFound.name ...


0

If you want to use Array.prototype.map you can write something like this: const paintings = [ { painting: "Mona Lisa" }, { painting: "The Starry Night" }, { painting: "The Last Supper" }, { painting: "Girl with a Pearl Earring" }, { painting: "American Gothic" }, { painting: "The Night Watch" } ]; const paintingsNew = { artworks: [] }; ...


5

Your code flattens the entire array of {} objects, indiscriminately flattening all properties. It does not mind, if there is only one single property, but it does not make sense semantically, especially if the array is named paintings. Rather consider the resulting array a projection/mapping to a single property painting. For a consistent array check and ...


Top 50 recent answers are included