lundi 31 août 2015

Find all numbers in a integer list that add up to a number

I was asked this question on an interview. Given the following list:

[1,2,5,7,3,10,13]

Find the numbers that add up to 5.

My solution was the following:

#sort the list:
l.sort()
result = ()
for i in range(len(l)):
    for j in range(i, len(l)):
         if l[i] + l[j] > 5:
             break
         elif l[i] + l[j] == 5:
             result += (l[i], l[j])

The idea I presented was to sort the list, then loop and see if the sum is greater than 5. If so, then I can stop the loop. I got the sense the interviewer was dissatisfied with this answer. Can someone suggest a better one for my future reference?



via Chebli Mohamed

Woocommerce Show default variation price

I'm using Woocommerce and product variations and all my variations have a default variation defined. I would like to know, how I can find the default variation and display it's price.

This is the code I got so far, but it displays the cheapest variation price, and I'm looking for my default variation product price instead.

// Use WC 2.0 variable price format, now include sale price strikeout
add_filter( 'woocommerce_variable_sale_price_html', 'wc_wc20_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'wc_wc20_variation_price_format', 10, 2 );
function wc_wc20_variation_price_format( $price, $product ) {
// Main Price
$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
$price = $prices[0] !== $prices[1] ? sprintf( __( 'HERE YOUR LANGUAGE: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
// Sale Price
$prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
sort( $prices );
$saleprice = $prices[0] !== $prices[1] ? sprintf( __( 'HERE YOUR LANGUAGE: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

if ( $price !== $saleprice ) {
    $price = '<del>' . $saleprice . '</del> <ins>' . $price . '</ins>';
}
return $price;

}

I found the code example here Woocommerce variation product price to show default



via Chebli Mohamed

Powershell parsing regex in text file

I have a text file:

Text.txt

2015-08-31 05:55:54,881 INFO   (ClientThread.java:173) - Login successful for user = Test, client = 123.456.789.100:12345
2015-08-31 05:56:51,354 INFO   (ClientThread.java:325) - Closing connection 123.456.789.100:12345

I would like output to be:

2015-08-31 05:55:54 Login user = Test, client = 123.456.789.100
2015-08-31 05:56:51 Closing connection 123.456.789.100

Code:

$files = Get-Content "Text.txt"
$grep = $files | Select-String "serviceClient:" , "Unregistered" |  
Where {$_ -match '^(\S+)+\s+([^,]+).*?-\s+(\w+).*?(\S+)$' } |
Foreach {"$($matches[1..4])"} | Write-Host

How can I do it with the current code?



via Chebli Mohamed

How to preprocess high cardinality categorical features?

I have a data file which has features of different mobile devices. One column with categorical data type has 1421 distinct types of values. I am trying to train a logistic regression model along with other data that I have. My question is: Will the high cardinality column described above affect the model I am training? If yes, how do I go about preprocessing this column so that it has lower number of distinct values?



via Chebli Mohamed

Meteor cursor counts and forEach

I need to add min and max fields into collection items in publish function and filter items by this fileds. I found the solution by using forEach for cursor:

Meteor.publish 'productsWithMinMax', (filter, options) ->
    Products.find(filter, options).forEach (p) =>
        p.min = Math.min p.price1, p.price2, p.price3, p.price4
        p.max = Math.max p.price1, p.price2, p.price3, p.price4

        if p.min && p.max && (p.max < p.mainPrice || p.min > p.mainPrice )
            @added "products", p._id, p

    Counts.publish @, 'numberOfProductsWithMinMax', Products.find(filter), {noReady: true}

    @ready()

But now Counts.publish returns wrong count for my cursor. How to count my cursor in this case?



via Chebli Mohamed

ES6 Import Folder

Does the ES6 module syntax allow you to import something from a folder?

For example if I have ./sub/main.js and I try import foo from './sub in the ./main.js file, will this work?

Could I get this sort of importing from a folder to work with babel and webpack?

Basically this would be the equivalent of __init__.py in Python.



via Chebli Mohamed

Is there a custom of distributing a key description with databases, like it is done with maps?

For a project with a more complicated relational database structure, one of the requirements is long-term stability of the SQL database. To achieve this, I did some searching for a standard way of describing certain paths through a relational database. For example, this would include a description on how to combine related tables or a hierarchy of the keys/IDs used in the database.

What I am trying to do is a description of the database, so that someone who just imports the database dump to a SQL server will know how what queries to write and what fields to use as keys. (This situation could arise if, for example, the software used with the database can no longer be run at some point in time.)

While I am aware this could be done with UML, I'd like to ask whether there is some sort of standard or custom of distributing databases in open source software with a description, like the key always printed on maps so that people know what each color and line type means. Thanks!



via Chebli Mohamed