Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Activity for hkotsubo‭

Type On... Excerpt Status Date
Answer A: How to group a flat list of attributes into a nested lists?
You could create a dictionary to map each attribute to its respective list of items. Then you get the dictionary values to create the final list. Something like this: ```python import re pattern = re.compile(r'attr\d+') just to simulate a "file" file = [ 'attr1 apple 1', 'attr1 banana 2',...
(more)
about 1 month ago
Answer A: How to log first n lines of a stack trace in Java?
It's not clear where this stack trace comes from (either from an exception or the current thread), but it doesn't matter, the way to do it is the same. Both `Exception`'s and `Thread`'s have the `getStackTrace()` method. For the current thread, you can use `Thread.currentThread().getStackTrace()`,...
(more)
about 2 months ago
Answer A: How to delete a remote branch in git?
The other answer already provides the more straighforward solution (`push` with `--delete` option). But there's an older syntax that also works: ```bash git push : ``` Note that there's the `:` character before the branch name. Example: ```bash git push origin :my-branch ``` This ...
(more)
about 2 months ago
Answer A: How to overwrite lines of STDOUT in Python?
The solution proposed by the other answer works, but there's a corner case. If the last message is shorter than the previous one, you might not get what you want. Example: ```python print("this is some text", end="\r") print("abc") ``` Testing this in a Linux terminal, the output is: ```n...
(more)
6 months ago
Answer A: Git: How to clone only a few recent commits?
> How do I clone the repository with only part of the history? It depends on what part you want. It's possible to have shallow clones (which is exactly what you need, only a part of the commit history), and the documentation says there are the following options: > `--depth ` > Create a shall...
(more)
8 months ago
Answer A: Why is git merge from rather than to?
I can't speak for the people who designed it, but I guess it was made this way because you can merge multiple branches all at once. Let's say I've created multiple branches: ```none C---D => b1 / / E---F => b2 | / A--B--G--H => master \ ...
(more)
9 months ago
Answer A: How to verify if a year is leap in Java?
There are many ways to do it, it depends on what data you already have and/or the Java version. I have only the year's numeric value If you already have a value as a number (`int` or `long`), and is using Java >= 8, you can use the `java.time.Year` class, which has the static method `isLeap...
(more)
about 1 year ago
Question How to verify if a year is leap in Java?
How to verify if a year is leap? Given a numeric value (such as `2023`) or some object that represents a date (such as `Date`, `Calendar`, `LocalDate`, etc), how can I do it?
(more)
about 1 year ago
Answer A: How to use grep to print only specific word from a string
I wouldn't use `grep`. As the other answers already said, it's not the right tool for this job. Considering your specific case (fields separated by `/`), `basename` is the most straighforward way, as stated in Dirk's answer. I just want to provide some other alternatives. `cut` The `cut`...
(more)
over 1 year ago
Answer A: What is [{options}] in JavaScript?
Given the link where the code comes from (based on your other question), this is just a, let's say, "free-form/pseudo-code/documentation example". It's not a valid JavaScript code. It's more like a free-format "syntax" intended to provide a general documentation about what parameters are expected...
(more)
almost 2 years ago
Answer A: How to make this treewalker code having a regular for loop or a forEach() method?
> I personally think its syntax is confusing. Well, it's just a matter of getting used to it, I guess :-) Anyway, this code is just looping through all `childNodes` and in each iteration it calls the `replaceIn` function, passing the element as argument. Therefore, you could replace the ...
(more)
almost 2 years ago
Answer A: How this recursive treewalker works?
> How this recursive treewalker works? To understand what the code does, we need to first see what problem it's trying to solve and why it needs to be solved this way. Let's consider this HTML: ```html Whatever site abcdef ``` It's rendered as: Rendered HTML And it's repr...
(more)
almost 2 years ago
Answer A: Recursion without a procedure ("function") in JavaScript
Yes, it's possible. In a more general way, every recursive algorithm can be converted to an iterative one (and vice-versa). You just need to use a stack). Internally, a recursive function will use a stack to "remember" its "level" (because each recursive call is like entering a new "level" - ju...
(more)
almost 2 years ago
Answer A: Does a for...of loop must contain a variable without assignment sign, and why?
> Does a `for...of` loop must contain a variable without assignment sign (`=`) Yes. > and why? Because that's the syntax defined by the language specification. The people who defined it decided to do it that way. Unless those people answer here (or we find a discussion about it somewhere,...
(more)
almost 2 years ago
Answer A: What are the types of DOM nodes?
> any DOM "tree" node is actually a "branch" Not exactly. Document Object Model and Nodes According to the MDN documentation, the DOM (Document Object Model) is "the data representation of the objects that comprise the structure and content of a document on the web." It's a hierarchical...
(more)
almost 2 years ago
Answer A: Delete all occurrences of a character in a webpage with vanilla JavaScript
Some comments and your answer are doing this: ```javascript document.body.innerHTML = document.body.innerHTML.replace(/x/g, ''); ``` But as explained in the comments, this is not a good solution, because it can break the page's HTML. Actually, even if you restrict it to a specific element (in...
(more)
almost 2 years ago
Answer A: Move to the edit webpage of a webpage via the keyboard with vanilla JavaScript
You're getting close, just need a few adjustments. Instead of using the `hostname` property, I prefer to use `host`, because it also includes the port (in case the URL has one) - check the documentation for more info. You should also get the `protocol` (which can be `http`, `https`, etc), to bu...
(more)
almost 2 years ago
Answer A: How to uncollapse the first and second tiers of a link tree in JavaScript?
According to the documentation, the `hasAttribute` method expects only one argument (the attribute's name), and it tells only if that attribute is present, regardless of its value. Hence, the return is a boolean (only `true` or `false`). When you pass more than one argument, only the first one is ...
(more)
about 2 years ago
Answer A: How can the Caesar cipher be implemented in Java?
Caesar Cipher originally deals only with letters (ASCII "A" to "Z", without diacritics/accents), so I'm not sure why you included `ۤ$%&` in your answer. And using `indexOf` (as you did) is not very efficient, because it always makes a loop through the string, until the character is found (which me...
(more)
about 2 years ago
Answer A: Mocking methods with arguments
Based on your answer, I guess you just wanted to have a list of unique arguments that were passed to `B::add`. In that case, you could use a `Set` instead of a `List`: ```java // "Type" is whatever type B:add receives as argument Set calledArgs = new LinkedHashSet<>(); @Test public void te...
(more)
about 2 years ago
Question Is it OK to use scanf with a void pointer?
I've created a function that calls `scanf` passing a void pointer as argument: ```c void read(const char format, void p) { scanf(format, p); } ``` And tested it with different types: ```c int n; read("%d", &n); printf("read int: %d\n", n); float f; read("%f", &f); printf("rea...
(more)
about 2 years ago
Answer A: When using the compare function in Array.prototype.sort, how to avoid an element to be processed more than once?
> PS: for small arrays and/or if the function is fast and doesn't cause performance bottlenecks, none of the below is really necessary (see the analysis in the end). That said, let's see how to solve it. I'm going to suggest two ways to make sure that the `getSortKey` function processes each eleme...
(more)
about 2 years ago
Question When using the compare function in Array.prototype.sort, how to avoid an element to be processed more than once?
When using the `Array.prototype.sort` method, we can pass a compare function as argument. Then, this function can be used to process array's elements, so the comparison is made using some custom criteria. But I noticed that this can lead to some, let's say, redundancy. For instance, this code: ...
(more)
about 2 years ago
Answer A: Why object-oriented instead of class-oriented?
> Why object-oriented instead of class-oriented? tl;dr Because you can "do OOP" without classes. Long answer A class is one possible way to implement object oriented programming. But it's not the only one. OOP is a programming paradigm: a particular "style"/way of designing program...
(more)
about 2 years ago
Answer A: How to deeply clone an array in Angular / TypeScript?
By "deeply clone", I assume you mean "also make copies of whatever nested structures the object might have". And for those, I guess libraries like Lodash are more reliable and appropriate if you want to save some work. Of course for simple cases (such as arrays containing only numbers, strings, or...
(more)
over 2 years ago
Answer A: What does a variable followed by parentheses ("ptr()") mean?
`void (ptr)()` defines a function pointer. It says that `ptr` is a pointer to a function. But that function must have a `void` return type, and take an arbitrary number of parameters (that's what the empty parentheses defines). Then, `ptr = PrintHello` assigns the `PrintHello` function to the `p...
(more)
over 2 years ago
Answer A: document.open() and the DOM tree of the loaded (closed) browser window on which it works
At the documetation you linked, if you click on "which will clear the document", it'll go to the documentation for `document.open`, and that page says in the beginning: > All existing nodes are removed from the document. And once removed, you can't retrieve them. Making a test in this page, ...
(more)
over 2 years ago
Answer A: Understanding createTreeWalker method in the context of replacing strings (or parts of them)
> How does storing replaced strings in the `node` variable makes a change in the text appearing to the end user? In your case, you're changing the `textContent` property. When accessed, it returns the text content of a node, concatenated with the text content of its descendants, and changing its v...
(more)
over 2 years ago
Question Code block in comments is highlighted only when viewed in the thread's link
Code blocks inside comments are hightlighted only if I'm on the comment's thread page. If I view it on the post itself (after expanding the respective thread), it's not highlighted. Example: in this post I clicked on the comment thread to expand it, and the code block is not highlighted: code...
(more)
over 2 years ago
Answer A: Change font-family with JavaScript
To do that, you could change the selector from `body` to ``, as the other answer said. By selecting only `body`, it won't change child elements that has defined a more specific rule, and that's why you need to set the style for all of them. But there are some corner cases that I think it's worth e...
(more)
over 2 years ago
Answer A: Separate digits of a number in groups with different sizes
Before we start, I'd like to be a little bit pedantic regarding `00002451018` being a number. When we talk about numeric types/values, the zeroes at the beginning are irrelevant: `2`, `02` and `000002` all refer to the number two. The numeric value is `2`, and only the representation - the way the...
(more)
over 2 years ago
Answer A: How do I support tab completion in a python CLI program?
It depends. Do you want to have autocomplete on the shell the program runs in, or do you want the program to intercept the TAB key and do the autocomplete by itself? Shell autocomplete If you're running your program in a Linux shell, and want to autocomplete in the shell's command line (such ...
(more)
over 2 years ago
Answer A: What is the purpose of `if __name__ == '__main__'`?
It makes difference if the script is being imported. Let's suppose I have a file `myfile.py`: ```python myfile.py def somefunction(): print('do some stuff') print('calling function:') somefunction() ``` If I execute it directly (such as `python myfile.py`), the output is: ```n...
(more)
over 2 years ago
Answer A: What is HEAD in Git?
First, we need to understand what a Git repository actually is. For that, refer to this article: it explains that a Git repository is actually a DAG (Directed Acyclic Graph). I'm not going into the mathematical details (which can be checked here), but basically, we can think of a Git repository as a ...
(more)
over 2 years ago
Question What is HEAD in Git?
In Git documentation, there are lots of references to the term "HEAD". But what exactly is it? Some places refer to it as "a pointer to the current branch". So it's a branch? What is it used for?
(more)
over 2 years ago
Answer A: What problem does innerHTML solves?
tl;dr According to the documentation, `innerHTML` property "gets or sets the HTML or XML markup contained within the element". Basically, "that's all", but let's see it in more detail. > It makes the element we work on to be copy-pasted into a new empty document No, the property itself d...
(more)
over 2 years ago
Answer A: Are there practical reasons for designing a method-only class/object?
It depends. Regarding "grouping functions/methods": as a general rule, you should group things that make sense to be together. Yes, it's a very broad and generic rule, and somewhat subjective. How you group those things, though, depends on a lot of factors. The other answers covered some aspect...
(more)
over 2 years ago
Answer A: What is the main difference between event delegation to other event handling patterns in JavaScript?
tl;dr The purpose of `addEventListener` is to define what happens when an event is triggered at some element. But it also allows us to implement event delegation, due to the bubbling/propagation behaviour. Explaining the terminology Bubbling Suppose I have this HTML: ```html A par...
(more)
over 2 years ago
Answer A: Why is the switch statement not executing the correct case blocks?
The problem is the fallthrough behaviour of `case` statements. Basically, once a `case`'s condition is met, all the others after that are also executed. Example: ```java int x = 2; switch (x) { case 1: System.out.println("one"); case 2: System.out.println("two"); ...
(more)
over 2 years ago
Answer A: What are the disadvantages of using static methods in Java?
> Is it better to use static method? I don't like to think of `static` (or any other language feature/mechanism) in terms of bad/worse and good/better (although I do that too, I constantly try to avoid it). What I usually try/prefer to do is to understand how something works, why it exists, the...
(more)
over 2 years ago
Answer A: How to run a remote JavaScript file from GitHub?
As you're using userscripts, I'm assuming this code is supposed to run in a browser. Hence, you could download the scripts and add its contents to the page's DOM (by using a `script` element). For that, you can use the Fetch API: ```javascript fetch('http://your.script.url', { cache: "no-store...
(more)
over 2 years ago
Answer A: How to make the text box such that its placeholder goes up and arranges itself in the centre of the border upon clicking?
The basic ideia is to create an `input` with a "fake" placeholder, and a `span` that will serve as the actual placeholder text. Then you group both inside a `label`, like this: ```html Enter your password ``` Note the `input`'s "fake" placeholder (just a single space). We'll use it ...
(more)
over 2 years ago
Answer A: What input functions can I use in TIO's PHP?
You could use `fgets` to read from `STDIN`. And to print the message, just use the short tag (``): ```php Try it online! Of course you could also do: ```php <?php echo 'Hello, '.fgets(STDIN).'!'; ``` And there are also other functions you could use, such as `filegetcontents("php://...
(more)
over 2 years ago
Question Is there a workaround to highlight code blocks if the language doesn't have syntax highlight enabled?
I've seen that recently two requests to add syntax highlight to some languages were deferred (this and this). According to the status-deferred tag description: "the requested feature will not be implemented in the near future". From that description, my understanding is that there's not a timel...
(more)
over 2 years ago
Answer A: How to parse a date with more than 3 decimal digits in the fractions of second?
The solution depends on the Java version you're using. First, let's see the solution for earlier versions, that doesn't use `SimpleDateFormat`. Then we'll see why the problem happens and alternatives for older versions. JDK >= 8 For JDK >= 8, you can (should/must?) use the `java.time` API. It...
(more)
over 2 years ago
Question How to parse a date with more than 3 decimal digits in the fractions of second?
I'm using `SimpleDateFormat` to parse a string containing a date/time, but the result has a different date, hour, minute, second and millisecond: ```java SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS"); Date date = sdf.parse("2021-10-01T10:30:45.123456789"); Syste...
(more)
over 2 years ago
Answer A: Function.prototype.call()
TL;DR To count the number of `li` items, just do: ```javascript console.log(document.querySelectorAll('ul.example > li').length); ``` That's all, no need to complicate with `filter.call` (but I'll explain that too, hang on). Adapt that to your HTML Of course you could change the se...
(more)
over 2 years ago
Question Enable syntax highlighting for D language
According to this answer, Codidact uses highlight.js for syntax highlight, and "support whatever languages are enabled by default in that package". And according to this table (in highlight.js GitHub), D language is enabled by default (no need for additional packages), but it also says that "our d...
(more)
over 2 years ago
Answer A: How to get string length in D?
> what ways can I get a string's length in D? There are many different ways, and that will depend on the content of the strings, their types, and how you define the terms "character" and "length". If you're dealing only with ASCII characters, using `length` - as pointed by the other answers - w...
(more)
over 2 years ago
Answer A: What's the difference between =, == and === operators in JavaScript?
Assignment (`=`) `=` is the assignment operator: it assigns a value to "something". One important detail is that an assignment expression not only assigns a value, but it also returns it. This allows chaining, such as: ```javascript x = y = z = 1; // all variables will be assigned the value...
(more)
over 2 years ago