Episode #125 of the Stack Overflow podcast is here. We talk Tilde Club and mechanical keyboards. Listen now

New answers tagged

4

What is wrong with simple code like?: distances <- adist(candidates, target) candidates[distances == min(distances)] This is shorter and, in my opinion, easier to read, as it do not require any knowledge of additional packages. Also, you mentioned that your data is small and it looks like you are working with vectors, in that case I do not see a point ...


1

You can use guava's Splitter class, splitting on & and with key/value separator =. By looking at the source code it seems the split implementation is O(n): import com.google.common.base.Splitter; import java.util.Map; import java.util.Map.Entry; ... String input = "key={value}&key2&key3={value3}"; Map<String, String> map ...


2

To be honest: Neither. Both are equally unnecessarily complicated and difficult to read. They use old-fashioned, maybe over-optimized techniques instead of Java/OOP features. The code lacks readable variable names and any documentation for the reader/reviewer or for other programmers who need to use this. And finally (if I read it correctly) they have ...


2

Avoid stringly-typed data Given this sequence of operations: Socket data(bytes) -> decode("utf-8")(string) -> decode_ascii6()(string) -> decode_msg_1()(dict) and based on the specification for AIS, UTF-8 is not an appropriate decode format. ASCII should be used instead - it's a fixed-width character format, and from what I read, none of the ...


2

I messed around with performance of some alternate implementations: #!/usr/bin/env python3 from collections import OrderedDict from string import ascii_lowercase from timeit import timeit def method_1(exclude): list_of_all_letters = list(ascii_lowercase) for char in exclude: if char in list_of_all_letters: list_of_all_letters....


2

I suggest you check PEP0008 https://www.python.org/dev/peps/pep-0008/ the official Python style guide and Flake8 http://flake8.pycqa.org/en/latest/ as a tool for style enforcement. Docstrings: Python documentation strings or docstrings are a way of documenting your classes/function/modules and are usually accessed through help(). You should include ...


0

Below is an answer highlight and performance comparison of various solutions. Note: the differences should be negligible in practice - if otherwise, timing variance is substantial, and all are ~on par (per benchmark) SOLUTION 1 GZ0-Mod+: def ordered_shuffle(*args): zipped_args = list(zip(*(a.items() if isinstance(a, dict) else a for a in args))) ...


7

While I do agree with others that Solution 2 is more readable with some improvements, there are also a few improvements that can be done on Solution 1. It is unnecessary to construct lists from iterables (e.g., generator expressions) when all that is needed is an iterable. For example, _args = [arg if type(arg)!=dict else arg.items() for arg in args] ...


1

The main attractions of a bitboard are, to put it shortly: Use the bit-parallel nature of bitwise operations to replace some simple loops. Use the power of arithmetic operations to replace non-trivial algorithms (eg o^(o-2r)). If a bitboard is emulated with a binary string, neither of those is realized. Effectively what you're dealing with then is a ...


11

functools.singledispatch functools library includes the singledispatch() decorator. It lets you provide a generic function, but provide special cases based on the type of the first argument. import functools import random @functools.singledispatch def shuffle(arg, order): """this is the generic shuffle function""" lst = list(arg) return type(...


7

I think your solution 2 is heading the right direction here. What I consider it's advantages over solution 1: It is much more readable It clearly shows that every member of *args is treated the same. You might want to generic-ify it a bit more to handle more types. For example, the following has a good chance of also handling custom container types: ...


2

Since you asked, I wouldn't think there would be any difference between using else vs ||. But, related to that, and to add on to the other answers, is that code should normally be written for humans first, and only incidentally for computers. It takes only a bit of mental effort to reason that (n % 2 != 0) || (n >= 6 && n <= 20) fulfils the ...


3

I think you want flatMap. Those returns and braces are unnecessary noise. I would consider not throwing but returning Optional<UserConfiguration> instead. Part of learning this style is realising that unwrapping is rarely necessary. At the call site, you can do something along the lines of update(user).ifPresentOrElse(...). [*] public Optional<...


3

In addition to the things dfhwze already mentioned: You should separate calculation and output. Even the shorter solution mentioned by dfhwze contains a repetition of the output code (System.out.println) and the output message ("Not weird"). Furthermore, thinking on to real world problems, code that only outputs something is really hard to unit-test. Thus, ...


2

As stated in the comments, this flow is simple enough for a compiler to optimize the code. So I wouldn't bother comparing performance between both methods. I would focus on readability and coding guidelines. Conventions You have customized formatting to somewhat align the System.out.println statements. While I consider this a form of art, it hurts ...


Top 50 recent answers are included