Python 3.5 Lists management [duplicate] - list

This question already has answers here:
What does "list comprehension" and similar mean? How does it work and how can I use it?
(5 answers)
Closed 6 years ago.
My question is not related to a problem, as such, but I looked at several python scripts, like the one below:
''.join([i if ord(i) < 128 else '' for i in text])
The list is built on a loop and contains an IF statement. I tried to find in the documentation the structure of such formula (e.g why put the IF in front and the FOR at the end). I am trying to understand the logic behind, in order to be able to build and develop my own formula. Unfortunately, despite all the documentation I looked on the net and books I bought, the information was pretty basic (usually they use enumerated lists and that's it). Could any of you give me a link to a doc that would be a bit more explicit on this topic ?
I recently discovered the dict(zip(a,b))-way to build dictionary, but the lack of understanding of this topic keeps me behind...
Best Regards,

Those are List Comprehensions and are pretty much a condensed for loop to cover common loop patterns with less code. ( https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions )

Related

Why is it not good practice to use 'goto'? [duplicate]

This question already has answers here:
What is wrong with using goto? [duplicate]
(6 answers)
Closed 5 years ago.
I read in the book I am learning C++ from about loops and it taught be goto first, but near the end it said not to use it, as it was bad practice, and said it created 'spaghetti' code. I don't understand what that means and why it is bad. Could someone please explain?
Spaghetti is a code poorly structured and which makes it hard to update because of multiple undocumented and unexpected links or dependencies. You touch it in one place and you see other things get broken or modified in an unexpected way. Just like sticky spaghetti, you pull one end and you see a number of places start moving.
GOTO usually violates the principles of structured, procedural programming, hence the term, which suggests the tangled and arbitrary nature of the program flow.

read function (and "create") from file (C++) [duplicate]

This question already has answers here:
Evaluating arithmetic expressions from string in C++ [duplicate]
(7 answers)
Closed 7 years ago.
i wonder if is possible, for example read a file with some content like:
a+b*c
and that my programm "create" a function to do this operation, and if i modify the file ( like a+b*c+2 ) the programm read this changes and updates what this function do. Well i dont have a solid backbround in the basis of C++ and i don't know if what i'm asking is just plain stupid. I need ( or something like this could be nice ) for my work in physics simulations, where the model is mainly definied by a equilibrium function ( and some other parameters ) so what i think is that could be good if i can make a programm to test this models without having to writte a special code for each one...
Thanks!
C++ is not interpreted code. So you can only compile hard coded expressions in source code. However you can evaluate an expression on your own. You can look at some solutions here
for sure it is "possible", actually that's what matlab, mapple or any other formal calculation software/lib do.
BTW, writing one may be quite simple if you just handle */+- basic operators, and may become more and more complex depending on what you want to use (sin cos, exp, log etc.)
Basic implementations reads of the input and build an internal tree with final values on leaves would look like soething like this in your case:
+
a *
b c
I'm sure you can find a lot of docs on it.

Can I make some text to load automatically whenever I open a file in Emacs? [duplicate]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
Particularly, what is the best snippets package out there?
Features:
easy to define new snippets (plain text, custom input with defaults)
simple navigation between predefined positions in the snippet
multiple insertion of the same custom input
accepts currently selected text as a custom input
cross-platform (Windows, Linux)
dynamically evaluated expressions (embedded code) written in a concise programming language (Perl, Python, Ruby are preferred)
nicely coexists with others packages in Emacs
Example of code template, a simple for loop in C:
for (int i = 0; i < %N%; ++i) {
_
}
It is a lot of typing for such common code. I want to invoke a code template or snippet which inserts
that boilerplate code for me. Additionally it stops (on TAB or other keystroke) at %N% (my input replaces it) and final position of the cursor is _.
TextMate's snippets is the most closest match but it is not a cross-platform solution and not for Emacs.
The second closest thing is YASnippet (screencast shows the main capabilities). But it interferes with hippie-expand package in my setup and the embedded language is EmacsLisp which I'm not comfortable with outside .emacs.
EDIT: Posted my answer here to allow voting on YASnippet.
Personally, I've been using Dmacro for years (ftp://ftp.sgi.com/other/dmacro/dmacro.tar.gz).
Here's a review of it that also mentions some alternatives: http://linuxgazette.net/issue39/marsden.html
The EmacsWiki has a page of template engines.
Of these, I've used tempo in the (distant) past to add table support to html-helper-mode, but don't know how it has progressed in the last 15 years.
I'd add my vote for tempo snippets ... easy to setup, powerful (you can run arbitrary elisp in your template - so that you can downcase things, lookup filenames & classes, count things, etc), set the indentation, integrate with abbrevs ... I use it a lot ;)
I vote for http://cedet.sourceforge.net/srecode.shtml
It has very clean syntax and has access to code environment through Semantic.
Also it is a part of a large well supported CEDET distribution (which was built into Emacs for 24.x version series).
UPDATE YASnippet is also a powerful template engine. But it uses an ugly file naming schema (your file name === template name) for you can't put several templates into a single file and have issues with national character sets...
You can try a lightweight solution muban.el
It is written completely in Elisp and has a very simple syntax.

What data structure would you use to implement a dictionary?

So I had an interview question about what data structure I would use to implement a dictionary. I said a map because you can enter the word and get the definition in return.
The follow up question was what data structure I would use to implement a system where the user only wanted to lookup a certain number of the starting letters of a word (i.e the first 3 letters) such that the user can get a list of, for example, all the words in the dictionary that start with fic.
I said something about a binary tree but really I had NO idea; the interviewer said the answer was TYPEAHEAD, something like what Microsoft Visual Studio's intellisense does. I had no idea what that was and I have tried to look it up on google but am getting some weird search results. Even though I have no idea what this is and never used it before it is definitely an interesting question.
Does anyone know how to do this? What kind of data structure and what would be the implementation method?
Edit: I'm confused why this is being closed. Is this not a programming question?
Use Trie, a type of tree. Follow this link for solution and better understanding: Trie

tool for generating an outline/map of a C++ code - is there such thing? [duplicate]

This question already has answers here:
C/C++ source code visualization? [closed]
(7 answers)
Closed 9 years ago.
I need to get into and make some modifications to a software component written in C++. I am fantasizing about generating some map of the code, that would show relationships between classes and walk me through the flow / call graph of methods. Is there a tool for this?
Years ago I worked with Rational Rose modeling tool with had a feature of reverse-engineering the code and building a class diagram for it. However what's important for such project exploration is also some dynamic information like sequence diagram (ideally) or call graph. Not mentioning that Rose is too big for such one off task and actually I don't know if it exists at all still.
I personally use Doxygen https://github.com/doxygen/doxygen and its truly among the easiest program to configure in a way that makes output like what you describe.
To generate call graphs you would also need dot which you can get in graphviz http://www.graphviz.org/. There might be some other dependency's but in those cases it should say so in the configuration file which by the way is rather well commented.
The configuration file of Doxygen might seem extensive at first, but the end result is worth it.
Warning, Douml was made from an old free version of BoUML (unfortunately not the last of them), when porting it in Qt4 the team introduced a lot of bugs, and at least because of that the result is unusable. Furthermore the team didn't worked on the plug-outs mechanism, so you aren't able to define you own plug-out etc. So it is better to get BoUML, it is not free but the price is very low compared to other UML tools. Zeks, BoUML has an automatic layout in the class diagrams. My two cents.
Take a look at BOUML, I think that's exactly what you're looking for:
http://www.bouml.fr/screenshots.html
If doxygen is not enough, I'd look into Enterprise Architect for the task. It's not free but it will generate your diagrams and code model. Although, tbh, I think doxygen is exactly what you need, and it's free to boot.
Btw, If you do decide to go Bouml way (generate code model, then make diagrams by hand), consider picking Douml from sourcefoge. Unlike Bouml, it's still free.