Tuesday, 29 January 2019

python - Word Searcher from Words.list



I was helping my mom with this word search app earlier while taking a break from some python tutorials and I realized -- what a perfect example of something interesting for me to work on/learn from.



Here's what I came up with, and while it works, I have to do some extra steps to figure out the words.




It seems that my double characters don't register as two, and one of them gets replaced with a non-relevant letter.



I've tried a lot of different things -- popping, removing. for character in words and not in myletters (to subtract out the difference). A lot of what I found involving this stuff were grids, and directionals, but I'm really just interested in a split list of words from my character input. Any help is appreciated.



Sidenote - I am very new to coding, so idk if this was the appropriate route. This x was x * 10 simpler than any example I could find. (Maybe why it doesn't work the way I want it to? ;p)



wordlist = open("PossibleWords.txt", "r").read().split()
myletters = input("Letters?: ").lower()
s = list()

sorted = str(myletters)

for word in wordlist:
if len(word) == len(myletters) and all(letter in word for letter in sorted):
s.append(word)
for length in s[:100]:
print(length)

Answer



Based on the OP's recent comment, it sounds like we're taking about word search. To not lose the double letters, we'll use Counter and compare counters:




from collections import Counter

def is_sub_count(counter_super, counter_sub):

counter_sub.subtract(counter_super)

return all(count <= 0 for count in counter_sub.values())

myLetters = input("Letters?: ").lower()

myCount = Counter(myLetters)
myLength = len(myLetters)

words = open("PossibleWords.txt").read().split()

found = list()

for word in words:
if len(word) <= myLength and is_sub_count(myCount, Counter(word.lower())):
found.append(word)


print(*found, sep='\n')


USAGE



> python3 test.py
Letters?: uassage
a
age

ague
as
ass
assuage
gas
gauss
guess
sag
saga
sage

sausage
sea
sue
us
usage
use
>

How do I convert a String to an int in Java?



How can I convert a String to an int in Java?



My String contains only numbers, and I want to return the number it represents.



For example, given the string "1234" the result should be the number 1234.


Answer



String myString = "1234";
int foo = Integer.parseInt(myString);



If you look at the Java Documentation you'll notice the "catch" is that this function can throw a NumberFormatException, which of course you have to handle:



int foo;
try {
foo = Integer.parseInt(myString);
}
catch (NumberFormatException e)
{

foo = 0;
}


(This treatment defaults a malformed number to 0, but you can do something else if you like.)



Alternatively, you can use an Ints method from the Guava library, which in combination with Java 8's Optional, makes for a powerful and concise way to convert a string into an int:



import com.google.common.primitives.Ints;


int foo = Optional.ofNullable(myString)
.map(Ints::tryParse)
.orElse(0)

php - Can I Install Laravel without using Composer?



I'd like to know if I can install or use the Laravel PHP framework on any web server without using Composer (PHP package/dependency manager) every time?



I would like to be able to drop my app on to any web server (like a shared server without access to the command line).



If I run composer install the first time (locally), then all the dependencies should be present, correct?




Then, I should be able to drop it onto any server with all of the files (including the vendor directory)?


Answer



If you really wanted to, you could do all the work that Composer does manually, but you definitely should not. Installing Composer is easy, it's just a matter of getting the composer.phar file and running commands on it.



You do not need to run Composer on your server as well as locally, once you run composer install or composer update your project will have all its dependencies available and you can just upload it straight to your server.


Checking if strings are equal in java using ==




when i execute the code below, the output is "false"



String string1 = new String("ABC");  
String string2= new String("ABC");
System.out.println(string1==string2);


However the output when I don't use the string class's constructor is "true"




String string1;
String string2;
string1="ABC";
string2= "ABC";
System.out.println(string1==string2);


I get that its better to use the .equals() methods but why the difference in output?


Answer




Always use equals since == doesn't always work. Even though objects are the same in memory it may be stored in different places, and == checks for objects identity and not equality.


performance - What setup does REP do?

The quote that you have given only applies to Nehalem microarchitecture (Intel Core i5, i7 and Xeon processors released in 2009 and 2010), and the Intel is explicit about it.



Before Nehalem, REP MOVSB was even slower. Intel is silent on what had happened in subsequent microarchitectures, but, then, with the Ivy Bridge microarchtecture (processors released in 2012 and 2013) Intel has introduced Enhanced REP MOVSB (we still need to check the corresponding CPUID bit) that allowed us to copy memory fast.




Cheapest versions of later processors - Kaby Lake "Celeron" and "Pentium", released in 2017, don't have AVX that could have been used for fast memory copy, but they still have the Enhanced REP MOVSB. That's why REP MOVSB is very beneficial on the processors released since 2013.



Surprisingly, Nehalem processors had quite fast REP MOVSD/MOVSQ implementation (but not REP MOVSW/MOVSB) for very large-sized blocks - just 4 cycles to copy each subsequent 64 bytes of data (if the data is aligned to cache line boundaries) after we've paid startup costs of 40 cycles - which is excellent when we copy 256 bytes and more, and you don't need to use XMM registers!



Thus, on Nehalem microarchitecture, REP MOVSB/MOVSW is almost useless, but REP MOVSD/MOVSQ is excellent when we need to copy more than 256 bytes of data and the data is aligned to cache line boundaries.



On previous Intel microarchitectures (before 2008) the startup costs are even higher.



Here are the tests of REP MOVS* when the source and destination was in L1 cache, of blocks large enough to not be seriously affected by startup costs, but not that large to exceed the L1 cache size. Source: http://users.atw.hu/instlatx64/




Yonah (2006-2008)



    REP MOVSB 10.91 B/c
REP MOVSW 10.85 B/c
REP MOVSD 11.05 B/c


Nehalem (2009-2010)



    REP MOVSB 25.32 B/c

REP MOVSW 19.72 B/c
REP MOVSD 27.56 B/c
REP MOVSQ 27.54 B/c


Westmere (2010-2011)



    REP MOVSB 21.14 B/c
REP MOVSW 19.11 B/c
REP MOVSD 24.27 B/c



Ivy Bridge (2012-2013) - with Enhanced REP MOVSB



    REP MOVSB 28.72 B/c
REP MOVSW 19.40 B/c
REP MOVSD 27.96 B/c
REP MOVSQ 27.89 B/c



SkyLake (2015-2016) - with Enhanced REP MOVSB



    REP MOVSB 57.59 B/c
REP MOVSW 58.20 B/c
REP MOVSD 58.10 B/c
REP MOVSQ 57.59 B/c


Kaby Lake (2016-2017) - with Enhanced REP MOVSB




    REP MOVSB 58.00 B/c
REP MOVSW 57.69 B/c
REP MOVSD 58.00 B/c
REP MOVSQ 57.89 B/c


As you see, the implementation of REP MOVS differs significantly from one microarchitecture to another.



According to Intel, on Nehalem, REP MOVSB startup costs for strings larger than 9 bytes are 50 cycles, but for REP MOVSW/MOVSD/MOVSQ they from 35 to 40 cycles - so REP MOVSB has larger startup costs; tests have shown that the overall performance is worst for REP MOVSW, not REP MOVSB on Nehalem and Westmere.




On Ivy Bridge, SkyLake and Kaby Lake, the results are the opposite for these instructions: REP MOVSB is faster than REP MOVSW/MOVSD/MOVSQ, albeit just slightly. On Ivy Bridge REP MOVSW is still a laggard, but on SkyLake and Kaby Lake REP MOVSW isn't worse than REP MOVSD/MOVSQ.



Please note that I have presented test results for both SkyLake and Kaby Lake, taken from the instaltx64 site just for the sake of confirmation - these architectures have the same cycle-per-instruction data.



Conclusion: you may use MOVSD/MOVSQ for very large memory blocks since it produces sufficient results on all Intel microarchitectures from Yohan to Kaby Lake. Although, on Yonan architectures and earlier, SSE copy may produce better results than REP MOVSD, but, for the sake of universality, REP MOVSD is preferred. Besides that, REP MOVS* may internally use different algorithms to work with cache, which is not available for normal instructions.



As about REP MOVSB for very small strings (less than 9 bytes or less than 4 bytes) - I would not even had recommended it. On the Kaby Lake, a single MOVSB even without REP is 4 cycles, on Yohan it is 5 cycles. Depending on context, you can do better just with normal MOVs.



The startup costs does not increase with size increase, as you have written. It is the latency of the overall instruction to complete the whole sequence of bytes that is increased - which is quite obvioius - more bytes you need to copy, more cycles it take, i.e. the overall latency, not just the startup cost. Intel did not disclose the startup cost for small strings, it did only specify for string of 76 bytes and more, for Nehalem. For example, take this data about the Nehalem:





  • The latency for MOVSB, is 9 cycles if ECX < 4. So, it means that it takes exactly 9 cycles to copy any string as soon as this string has 1 byte or 2 bytes or 3 bytes. This is not that bad – for example if you need to copy a tail and you don’t want to use orverlapping stores. Just 9 cycles to determine the size (between 1 and 3) and actually copy the data – it is hard to achieve this with normal instructions and all this branching – and for a 3-byte copy, if you didn’t copy previous data, you will have to use 2 loads and 2 stores (word+byte), and since we have at most one store unit, we wont’ do that much faster with normal MOV instructions.

  • Intel is silent on what latency has REP MOVSB if ECX is between 4 and 9

  • Short string (ECX <= 12): the latency of REP MOVSW/MOVSD/MOVSQ is about 20 cycles to copy the whole string – not just the startup cost of 20 cycles. So it takes about 20 cycles to copy the whole string of <= 12 bytes, thus we have a higher thoughoutput rate per byte than with REP MOVSB with ECX < 4.

  • ECX >= 76 with REP MOVSD/MOVSQ – yes, here we DO have startup cost of 40 cycles, but, this is more than reasonable, since we later use copy each 64 bytes of data at just 4 cycles. I’m not an Intel engineer authorized to reply WHY there is a startup costs, but I suppose that it is because for these strings, REP MOVS* uses (according to to Andy Glew's comments on an answer to Why are complicated memcpy/memset superior? from the Peter Cordes’ answer) a cache protocol feature that is not available to regular code. And there comes an explanation at this quote: „The large overhead for choosing and setting up the right method is mainly due to the lack of microcode branch prediction”. There has also been an interesting note that Pentium Pro (P6) in 1996 implemented REP MOVS* with 64 bit microcode loads and stores and a no-RFO cache protocol - they did not violate memory ordering, unlike ERMSB in Ivy Bridge.

Prefix operator difference in C++ and C#

Let's have this piece of code:




int a = 1;
int b = ++a + ++a;


In C++ (VS 2010) the result is: b = 6
but in C# the result is: b = 5



What's going on there? Why are the results different?

plot explanation - If Wolverine's skeleton is completely bonded with Adamantium, why are his teeth still white?

In "X-Men Origins Wolverine", Logan undergoes an operation to have Adamantium bonded to his skeleton.


Why doesn't the Adamantium bond to his teeth?


php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...