Find substring count without string functions Find the substring count from a string without string functions in Java. Given String str = "abcdefghcde"; and String find = "cde";, count occurrences of "cde" in String str.

Taking into account overlaps.

Example: String str = "cdcdcdc"; and String find = "cdc";, occurrence count is 3

Please give any feedback about improving it or any issues (performance or otherwise) that you see.

String input = "cdcdcdcdcdcddc";
    String find = "cdc";
    StringBuffer found = new StringBuffer();
    int count = 0;
    int j=0;
    for ( int i = 0; i <input.length();){
            j = 0;
            while(j<find.length() && i<input.length() && input.charAt(i) == find.charAt(j)) {
                found.append(input.charAt(i));
                j++;
                i++;
            }
            i=i-j;
            if(found.toString().equals(find)) {
                count++;
            }
            i++;
            found.setLength(0);
        }
    System.out.println("count is " + count);
    }

I have another question here which doesn't take into account overlaps and it's complexity is \$O(n)\$. But for this question, I'm unable to achieve \$O(n)\$ time running time.

Update: For all the people down voting, this is not a duplicate but a variant of the other question. If posting variants of a question is against the rules of stackexchange, please let me know and I'll delete my other post. Otherwise, I'd like to keep the other post because my solutions are slightly different in approach and much different in complexity and I'd like a feedback on both of them. Posting them as part of one post would be messy, both for me, and for the people trying to give feedback.

Efficiency

If you want to badly enough, I'm pretty sure you can improve the efficiency of your current code, at least for typical cases--but I don't think the result will be \$O(N)\$ even if you do.

For the moment, let's treat \$N\$ as the length of the string you're searching in (which I'm going to call the haystack), and \$M\$ as the length of the string you're searching for (which I'm going to call the needle).

As you've implemented it, this is an \$O(MN)\$ operation. That is, there are \$N\$ positions in the haystack where the needle could be found, and \$M\$ comparisons to verify whether you've found the needle at that location.

The obvious way to reduce the complexity would be to use something like a Boyer-Moore search. A full Boyer-Moore search is fairly tricky to get correct, but you could use (for example) Sunday's variant of Boyer-Moore-Horspool.

So, let's start by describing the basic idea of B-M searching. Unlike most string searching, B-M based searches start from the end of the needle. For example, let's assume I'm doing a search like:

Needle: abcde
Haystack: abcabcdqwerpuiasdfasdfabcdeyakkfdabcdessity

Now, if there were a match at the first position in the haystack, the first characters would obviously have to be abcde. Using your method (starting from the beginning), we'd start from the beginning of the needle, and do four comparisons before we found that this wasn't a match (i.e., the a, b and c all matched, to it wasn't until we got to the next a that we knew it couldn't match.

With any of the Boyer-Moore variants, we'd start by looking at the fifth character in the haystack. We can't have a match at the first position unless that matches. It doesn't, so we can reject the first position with only one comparison.

Now, what we'd like to do next would be to jump ahead to the next position where a match is possible. We can look at the character there (a b, if I'm counting correctly). Looking at our needle, we know that only occurs at the second position, so we can jump ahead three more, and look at that character. That's a q, which doesn't occur in the needle, so we can jump ahead 5 more characters after that. Lather, rinse, repeat.

To manage that jumping, we don't want to scan through our needle every time to find where (of if) a character occurs in it. To avoid that, we build a table of character positions. The elements in the table will be the distance each character is from the end of the needle (and if the character doesn't occur in the needle, it'll be the length of needle + 1). That'll give us the location where we need to look for our next possible match.

When/if we get to a point where we've hit a zero in the table, we do a full comparison to see if we've found a location where the needle occurs.

abcabcdqwerpuiasdfasdfabcdeyakkfdabcdessity
abcde
   abcde
        abcde
             abcde
                  abcde
                     [abcde]

Now, there is a little bit of trickiness to deal with. This occurs if the needle contains the same character at two (or more) locations. For example, if the needle were literally needle, we have three locations where an e could indicate a match.

This is where the variants start to show up. The original Boyer-Moore built a rather complex 2D table with all those possible jumps encoded into it. The variants simplify that to at least some degree, gaining quite a bit in simplicity in exchange for a loss of efficiency (that's usually fairly minor).

In the variant I prefer, we (as pointed out above) only start to do a comparison when we reach a point where we've found a 0 in the shift table. In this case, we don't need a table of other locations where that occurs. We just need one secondary jump to the next possible match location for that one character. In this case, that would be 3 because it's three characters back from the last e to the second to last e in needle.

Looking at overall complexity, this can be a lot lower, especially if our needle is long and consists of unique characters. On the other hand, if it's short and consists entirely of repeated characters (e.g., cc) we're not going to gain much at all. At the same time, if the needle is short, the \$O(MN)\$ method is already close to the \$O(N)\$ we'd like. It's only when \$M\$ is large that the \$O(MN)\$ method becomes a serious problem--and that's exactly when a Boyer-Moore (or variant) search is most likely to give us the biggest gain (the primary exception being when/if the needle is long, but contains lots of repetition). Since big-O deals with the worst case, I think this is still \$O(MN)\$--but that only arises if the needle consists entirely of repetitions of a single character, and the haystack consists almost entirely of that same character as well. That's  fairly degenerate (and probably unusual case, but to use @Coderodde's example, if you're searching for cc in cccc, all the work is mostly wasted, because you're going to do a full comparison at every candidate position in the haystack, so it still ends up as \$O(MN)\$.

Your code

Looking at your code, I'm most puzzled by your use of the found buffer. You're basically copying characters from the input to found as long as the two match, then checking the length of found to see if you've gotten a match.

Instead of that, I think I'd move the check for a match at the current position into a separate function, which would implement an algorithm on this general order:

for pos = 0 to needle.length()
    if (hay_stack[start_pos + pos] != needle[pos])
        return false
return true


Setting color of specific keywords in a textview I'm developing an Android app to view C-programs.  I wanted to give a simple color scheme to the text which is stored in database, retrieved as a string and then passed on to the textview.

The code I have written assigns the green color to header file declarations and brackets, the blue color is assigned to numbers, printf, scanf, and the red color is assigned to datatypes such as int, char, float.

It is, however, very inefficient.  Before applying this color scheme, my app was displaying the textview activity instantly.  Now, depending on the length of the C-programs, it takes up to 4 to 5 seconds, which is really poor performance.

It takes one keyword at a time, then iterates the complete text of textview looking for that particular keyword only and changes its color, sets the text again.  Thus, it traverses text of the entire textview 29 times as I have defined 29 keywords in String arrays (namely keywordsgreen, keywordsblue, keywordsred).

The activity's onCreate() function:

textView  = (TextView) findViewById(R.id.textView1);
textView.setText(programtext);
textView.setBackgroundColor(0xFFE6E6E6);

//The problem starts here

String [] keywordsgreen={"#define","#include","stdio.h","conio.h","stdlib.h","math.h","graphics.h","string.h","malloc.h","time.h","{","}","(",")","<",">","&","while ","for "};
for(String y:keywordsgreen)
{
fontcolor(y,0xff21610B);
}

String [] keywordsred={"%d","%f","%c","%s","int ","char ","float","typedef","struct ","void "};
for(String y:keywordsred)
{
fontcolor(y,0xFFB40404);
}

String [] keywordsblue={"printf","scanf","\n","getch","0","1","2","3","4","5","6","7","8","9"};
for(String y:keywordsblue)
{
fontcolor(y,0xFF00056f);
}

The fontcolor() function:

private void fontcolor(String text,int color)
{
    Spannable raw=new SpannableString(textView.getText());
    int index=TextUtils.indexOf(raw, text);
    while (index >= 0)
    {
      raw.setSpan(new ForegroundColorSpan(color), index, index + text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      index=TextUtils.indexOf(raw, text, index + text.length());
    }
    textView.setText(raw);
}

First of all, some side notes:

In your keywordsgreen array, you're checking for very specific library names. But you also check for #include and #define. So what if I include <stdlib.h>? Your current code would search the text, find #include, turn it green, then search again, find <stdlib> and turn that green. But the whole line might as well be green. Instead, just look for the # symbol at the absolute start of the line (it would take a little more logic, of course). That way, you eliminate most of the keywordsgreen array.
Also, you're setting the text every single time you edit a single word. You don't need to do that. Just keep one SpannableString and pass that around, setting the textView to that all the way at the end of the function.
I don't think this should be that slow. It's not a very good solution, but even for a 300-line source file, I can't imagine it would take 5 seconds. Put all that in a new thread and see what happens.

Example:

Thread colorTextThread = new Thread(new Runnable(){
public void run(){
  //Put all that searching code in here
}//end of run
});
colorTextThread.start();

You put it on a new thread because the UI thread is super slow. Remember, though, if you want to change the textView on a separate thread, you have to use the runOnUiThread() function.

I'll add more later if no one tells you how to make the code faster. I've got to go at the moment, sorry.

EDIT:

After looking at your code again, I can't see a way to make it faster. The most important things you can do is to do the search on the "programText" variable you're passing to textView. That variable holds all the information you need, so you should manipulate that, and then pass it to textView.setText(). That way, you never have to call getText(), which means you can do all of your text processing on a separate thread. If it runs slow even on a separate thread, then yes, your code is very slow. Otherwise, problem solved for now.


Map price values to bins where bins are of a larger price range I have two different pandas data frames where in the first data frame (price), I have two columns. The first column named value has some values in it and the second column amount has the available amount for each price. The second data frame (bins) has as index some price intervals which are produced from the price data frame. For each row of the price data frame I check each row of the value column to find the interval that it belongs from the bins data frame and if the value is in an interval I assign the available amount in the bins data frame. If another value is again in the same interval I sum up these amounts in the group_bins data frame.

import pandas as pd

bins = pd.DataFrame({
    'value': [1, 2, 5, 7, 8, 16, 20, 3, 9, 11, 35, 12, 54, 33, 3, 22, 23]
})

price = pd.DataFrame({
    'value': [2, 5, 7, 8, 16, 20, 3, 9, 11, 2.5, 3.4],
    'amount': [50, 112, 130, 157, 146, 148, 300, 124, 151, 100, 32]
})

bins['bins'] = pd.qcut(bins['value'], 12)
group_bins = bins.groupby(['bins']).sum()
group_bins['amount'] = 0
del group_bins['value']

for j in range(price.shape[0]):
    for i in range(group_bins.shape[0]):
        if price.loc[j, 'value'] in group_bins.index[i]:
            group_bins.loc[group_bins.index[i], 'amount'] += price.loc[j, 'amount']

            break

Expected Result:

                 amount
bins
(0.999, 2.333]        50
(2.333, 3.0]         400
(3.0, 5.0]           144
(5.0, 7.333]         130
(7.333, 8.667]       157
(8.667, 11.0]        275
(11.0, 13.333]         0
(13.333, 18.667]     146
(18.667, 22.0]       148
(22.0, 26.333]         0
(26.333, 34.333]       0
(34.333, 54.0]         0

My problem is that I have 100k data and all this process takes too long to finish.
Is there any elegant and much faster way to replace these nested for loops and the if condition?

The expected result is the final column in the group_bins amount column. Any help would be much appreciated! Thank you.

This can be solved using groupby directly. First, get the bins you want to use, then just pandas.cut the actual values into those bins.

binning = pd.qcut(bins['value'], 12, retbins=True)[1]
group_bins = price.groupby(pd.cut(price.value, binning)).amount.sum()

This produces basically the same output for the given example, except that it is a pandas.Series instead of a pandas.DataFrame:

value
(1.0, 2.333]         50
(2.333, 3.0]        400
(3.0, 5.0]          144
(5.0, 7.333]        130
(7.333, 8.667]      157
(8.667, 11.0]       275
(11.0, 13.333]        0
(13.333, 18.667]    146
(18.667, 22.0]      148
(22.0, 26.333]        0
(26.333, 34.333]      0
(34.333, 54.0]        0
Name: amount, dtype: int64

In addition, you should probaly put this into a function and only call it from a if __name__ == "__main__": guard to allow importing from the file.

I am not quite sure why you need to determine the binning the way you do, though. From your variable names I would have assumed that bins.value are the bin edges to be used instead.


Minesweeper grid representation + calculating neighbor-sums I'm trying to get better at Python and would like a critique.

Problem statement:

  Accept a 2D grid containing 1's and 0's (1 being mine locations) and
  print a solved sum grid (the way the final grid would show upon
  completion of a minesweeper game with all the neighbor sums).

Beyond that, what might be horribly wrong with the code below?  I am interested in finding a recommended balance between readability and what the cool kids call idiomatic. For example, List comprehension in some places was deliberately omitted in favor of making something that I thought was easier on the eyes.

The top-level functions are called via:

if __name__ == '__main__':
    locMap = [[1,0,0,0],[0,0,1,0],[0,1,0,1]] # Sample input
    print "Location map = "
    printMineField(locMap,locMap)
    newMap = getSummedMap(locMap)
    print "Summed map = "
    printMineField(locMap,newMap)

The output is:

'''

Location map =
* 0 0 0
0 0 * 0
0 * 0 *

Summed map =
* 2 1 1
2 3 * 2
1 * 3 *

'''

The actual functions are:

def generatePaddedGrid(grid):
    """
    Input: 2D list.
    Output: 2D list
    Purpose: Returns a 0-padded boundary around a grid.  Useful to perform
    easier 2D traversal + operations of the inner grid without painful
    boundary checking
    """

    # Get Dimensions of grid
    nRows = len(grid)
    nCols = len(grid[0])

    # Create a grid with +2 row/col padding (on all sides)
    paddedGrid = [[0 for x in range(0,nCols + 2)] for x in range(0,nRows + 2)]

    # Insert 'centered' locMap into the Padded Grid
    for xIter in range(0,nRows):
        for yIter in range(0,nCols):
            paddedGrid[xIter+1][yIter+1] = grid[xIter][yIter]

    return paddedGrid

def getSummedMap(locMap):
    """
    Input: 2D list
    Output: 2D list
    Purpose: Takes in a 2D array (locMap)   and returns a same-sized array
    with each cell now containing the sum of all the neighbors including
    diagonals (max of 8 neigbor cells)
    """

    # Insert + Center this grid into a padded grid to prevent access errors
    paddedGrid = generatePaddedGrid(locMap)

    nRows = len(locMap)  #Actual rows to be iterated
    nCols = len(locMap[0]) #Actual cols to be iterated.

   # Create a target/output grid of the actual size to write the sums into
    sumGrid = [[0 for x in range(0,nCols)] for y in range(0,nRows)]

    for xIter in range(1,nRows+1):
        for yIter in range(1,nCols+1):
            Sum = 0
            # Top + Bottom + Left + right
            Sum  += paddedGrid[xIter-1][yIter] +\
                    paddedGrid[xIter+1][yIter] +\
                    paddedGrid[xIter][yIter+1] +\
                    paddedGrid[xIter][yIter-1]

            # Then 4 diagonals
            Sum  += paddedGrid[xIter-1][yIter-1] +\
                    paddedGrid[xIter+1][yIter-1] +\
                    paddedGrid[xIter+1][yIter+1] +\
                    paddedGrid[xIter-1][yIter+1]

            sumGrid[xIter-1][yIter-1] = Sum

    return sumGrid

def printMineField(locMap, SumMap):
    """
    Input: 2 x 2D Arrays
    Output: No return, just print an overlayed Sum-map over a given Loc-map
    but leave the mine-locations as '*' (instead of the neighbor-sums)
    """

    numActualRows = len(locMap)  #Actual rows to be iterated
    numActualCols = len(locMap[0]) #Actual cols to be iterated.

    for xIter in range(0,numActualRows):
        for yIter in range(0,numActualCols):
            if locMap[xIter][yIter] == 1:
                print '*',
            else:
                print str(SumMap[xIter][yIter]),
        print '\n'

One idea ... in your getSummedMap function, you could put all the +1, -1, 0's in a list of 2-tuples and get the Sum with one for loop, e.g. ...

neighbor_incs = [(-1, 0), (1, 0), (0, 1), (0, -1),  # Top + Bottom + Left + right
                 (-1, -1), (1, -1), (1, 1), (-1, 1), ] # diagonals

for x_inc, y_inc in neighbor_incs:
    Sum += paddedGrid[xIter + x_inc][yIter + y_inc]


int to Roman numerals in Go / Golang For a new project, I've started learning Golang. This is one of my very first steps in Go.

The objective that I've given myself is to convert int to a String representing the roman numeral of that int. The supported range shall start with 0, negative numbers can be ignored, and it's okay for now that for negative numbers, an empty String is returned.

File roman_test.go

package main

import "testing"

func TestRoman(t *testing.T) {
    testcases := []struct {
        number int
        roman string
    }{
        { 0, "" },
        { 1, "I" },
        { 2, "II" },
        { 4, "IV" },
        { 5, "V" },
        { 1993, "MCMXCIII" },
        { 2018, "MMXVIII" },
        { 1111, "MCXI" },
        { 2222, "MMCCXXII" },
        { 444, "CDXLIV" },
        { 555, "DLV" },
        { 666, "DCLXVI" },
        { 999, "CMXCIX" },
    }

    for _, testcase := range testcases {
        roman := Roman(testcase.number)
        if roman != testcase.roman {
            t.Errorf("%d expected to convert to %s, got: %s.", testcase.number, testcase.roman, roman)
        }
    }
}

File roman.go

package main

func Roman(number int) string {
    conversions := []struct{
        value int
        digit string
    }{
        {1000, "M"},
        {900, "CM"},
        {500, "D"},
        {400, "CD"},
        {100, "C"},
        {90, "XC"},
        {50, "L"},
        {40, "XL"},
        {10, "X"},
        {9, "IX"},
        {5, "V"},
        {4, "IV"},
        {1, "I"},
    }

    roman := ""
    for _, conversion := range conversions {
        for number >= conversion.value {
            roman += conversion.digit
            number -= conversion.value
        }
    }
    return roman
}

I'm mainly interested in feedback on the following aspects:

Go code style
Idiomatic go
Better structure

This is some fantastic first Go code!

Great use of anonymous structs for test fixtures. I disagree with @sineemore, these are incredibly idiomatic. Nice job writing tests, as well!

There isn't much to be said for the code itself. It's simple and reads clearly. Perhaps you'd want to document it (go doc is fantastic).

I guess strings.Builder is the idiomatic way to do string concatentation in Go >=1.10. However, this is largely for performance reasons.

var roman strings.Builder
for _, conversion := range conversions {
    for number >= conversion.value {
        roman.WriteString(conversion.digit)
        number -= conversion.value
    }
}
return roman.String()

Your next steps are learning about packaing and naming. I'd suggest that instead of calling the field digit you call it something more descriptive like numeral (conversion.numeral reads better than conversion.digit to me). Perhaps you can also come up with better names for roman and number?

I'd also give the function a better name. And to do so, I'd place it in a package called roman. Then call it FromInt. In this way, you use it like roman.FromInt(42), which reads much better. Package naming is really important in Go and you'll find a lot of the standard libraries follow this pattern of making the package name part of the "sentence" instead of just some extra stuff you have to prepend.

In doing this, I'd also recommend putting your tests in package roman_test so they can't access any private functions or variables. This is convention.


Simple class exercise using the "this" pointer This is my solution for an exercise from a book.  It's a simple Golf class with member variables fullname and handicap. The constructor sets the variables to the provided name and handicap using values passed as arguments. The setgolf() function solicits name and handicap from user, creates a temporary Golf object and assigns it to the invoking object.

Is this a proper solution? I'm particularly interested in the setgolf() method.

golf.h:

#include <string>

class Golf
{
    private:
        std::string fullname;
        int handicap;
    public:
        Golf();
        Golf(const std::string name, int hc);
        int setgolf();
        void sethandicap(int hc);
        void showgolf();
};

golf.cpp:

#include "golf.h"
#include <cstring>
#include <iostream>

Golf::Golf(const std::string name, int hc)
{
    fullname = name;
    handicap = hc;
}

int Golf::setgolf()
{
    std::string name;
    int hc;
    std::cout << "Enter the name: ";
    getline(cin, name);
    if (name == "")
        return 0;
    std::cout << "Enter the handicap: ";
    std::cin >> hc;
    *this = Golf(name, hc);
    return 1;
}

It's hard to say whether the solution is proper when there isn't a problem to solve.  How a class should be designed depends on how it will be used; a class in isolation is hard to judge.  Even if you want to focus on some specific aspect, try to fit it into the context of a larger problem.

Back to the code: I suspect showgolf should be a const function.  I don't see why printing a Golf would change it.  You probably also meant to pass your constructor a const std::string&.  Finally, are you sure a showgolf member function is really what you want -- why not an operator<< overload?

Moving on to the Golf::setgolf function specifically, I don't think such a function is meaningful.  An operator>> overload or a free queryGolf function sound significantly more sensible to me.  More specifically, the usage:

Golf golf;
golf.setgolf();

Why not just auto golf = queryGolf();?  It's simpler, it means you can't forget to check whether things succeeded, and given setGolf's implementation it is likely to be just as efficient.

Speaking of checking whether things succeeded, why not throw an exception if the input is invalid?  There are valid reasons, but it's certainly a solution you should consider.  (By the way, if my previous solution sounded bad due to the need for exceptions, have no fear: boost::optional fits the use-case very well.)

And, in a similar vein: it sounds like you're trying to prevent the creation of invalid Golfs.  However, your default constructor does exactly that.  Are you sure default-construction is more valuable than guaranteed validity?  Again, depending on what you do with the class, the answer varies.

All in all, I'd suggest dropping showgolf and writing:

Golf promptGolf() {
    Golf golf;
    // read into fullname and handicap.  promptGolf may be a friend.
    if (/* golf is not valid, std::cin died, etc. */)
        throw TerribleError{};
    return golf;
}


Check the stock of references list I did this spider that checks the stock of references list (around 3500 references).

Now the spider takes around 37 seconds to scrap 400 references. The CPU is around 5%, the network card (1Gbps) around 18%. My internet connection is 300Mbps simetric and it's only connected to this computer.

Any idea to improve performance?  Is this a good performance? Maybe ISP router is a bottleneck?

import scrapy
from scrapy.crawler import CrawlerProcess

class Spider(scrapy.Spider):
    name = "Spider"

    start_urls = ['URLS']

    def __init__(self, references=None, *args, **kwargs):
        super(ktmSpider, self).__init__(*args, **kwargs)

    def parse(self, response):
        return scrapy.FormRequest.from_response(
            response,
            formdata={'UserName': 'username', 'Password': 'password'},
            callback=self.after_login
        )

    def after_login(self, response):
        ref = references.pop()
        yield scrapy.Request(url="url" + ref, callback=self.parse_stock)

    def parse_stock(self, response):
        self.f.write(response.selector.xpath('//*[@id="priceDetails"]/form/div[2]/text()').extract_first() + ',')
        self.f.write(response.selector.xpath('//*[@id="priceDetails"]/form/div[8]/div[1]/span/span[2]/text()').extract_first() + ',')
        self.f.write(response.selector.xpath('//*[@id="priceDetails"]/form/div[8]/div[1]/span/span[1]/i/@style').extract_first() + '\n')

        while len(references) > 0:
            ref = references.pop()
            yield scrapy.Request(url="url" + ref, callback=self.parse_stock)

f = open("references.txt")
references = f.read().splitlines()

process = CrawlerProcess({
    'USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)',
    'LOG_LEVEL': 'INFO',
    'AUTOTHROTTLE_ENABLED': 'True',
    'AUTOTHROTTLE_START_DELAY': '0.1',
    'AUTOTHROTTLE_TARGET_CONCURRENCY': '100'
})

process.crawl(ktmSpider, references=references, stockFile="file.txt")
process.start()

Auto-throttling extension may cause high download delays. Either turn the extension off to see what would the resulting time be, or limit the maximum delay via AUTOTHROTTLE_MAX_DELAY.

Also, you may issue multiple requests from the after_login() method instead of keeping the queue of references:

def after_login(self, response):
    for ref in references:
        yield scrapy.Request(url="url" + ref, callback=self.parse_stock)

Also, instead of writing to the file from a spider directly, you can use a CSV output pipeline:

import csv

class CSVWriterPipeline(object):
    def __init__(self):
        self.writer = csv.writer(open('file.txt', 'wb'))

    def process_item(self, item, spider):
        self.writer.writerow([item["field1"], item["field2"], item["field3"])
        return item

Where field1, field2, field3 are your item fields:

class MyItem(Item):
    field1 = Field()
    field2 = Field()
    field3 = Field()

Which should be set in the parse_stock() callback:

def parse_stock(self, response):
    item = MyItem()
    item["field1"] = response.xpath('//*[@id="priceDetails"]/form/div[2]/text()').extract_first()
    item["field2"] = response.xpath('//*[@id="priceDetails"]/form/div[8]/div[1]/span/span[2]/text()').extract_first()
    item["field3"] = response.xpath('//*[@id="priceDetails"]/form/div[8]/div[1]/span/span[1]/i/@style').extract_first()
    return item

Then, you would need to enable the pipeline:

process = CrawlerProcess({
    'ITEM_PIPELINES', {
         '__main__.CSVWriterPipeline': 100
    },
    'USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)',
    'LOG_LEVEL': 'INFO',
    'AUTOTHROTTLE_ENABLED': 'True',
    'AUTOTHROTTLE_START_DELAY': '0.1',
    'AUTOTHROTTLE_TARGET_CONCURRENCY': '100'
})

Or course, improve the "field" and "item" class names to be more meaningful.

There are other things, like using local DNS cache, you may try to speed up your spider:

Speed up web scraper

And, as a "running Scrapy from script" reference topic, please see this post.


Computing average market spread in C# My task is to compute average half spread in percentages on a stream of market. This is part of the coding assignment that I have received.

namespace CodingAssignment
{
    public interface IMarketDataFeed
    {
        string StockName { get; }
        event Action<MarketDataUpdate> Update;
    }

    public interface IIndicator
    {
        event Action<double> IndicatorUpdate;
    }

    /// <summary>
    /// Computes the average half spread (ie bid to mid) in percentage on a stream of market data updates
    /// </summary>

    public class AverageHalfSpreadIndicator
    {
        private List<MarketDataUpdate> _lastUpdates;

        public AverageHalfSpreadIndicator(IMarketDataFeed marketDataFeed)
        {
            marketDataFeed.Update += OnMarketDataUpdate;
        }

        private void OnMarketDataUpdate(MarketDataUpdate updates)
        {
            _lastUpdates.Add(updates);
            if (_lastUpdates.Count == 20)
            {
                _lastUpdates.RemoveAt(0);
            }

            var start = DateTime.Now;
            var spreads = _lastUpdates.Select(x => 100 * (x.Mid - x.Bid) / x.Bid);
            Console.WriteLine("Time to compute spreads: "+(DateTime.Now - start).TotalMilliseconds+" ms");

            var averageSpread = spreads.Average();
            Console.WriteLine("Total time:" + (DateTime.Now - start).TotalMilliseconds + " ms");

            IndicatorUpdate(averageSpread);
        }

        public event Action<double> IndicatorUpdate;
    }

    public class MarketDataUpdate
    {
        public double Bid { get; set; }
        public double Ask { get; set; }
        public double Last { get; set; }

        public double Mid
        {
            get { return (Bid + Ask) / 2; }

        }
    }
}

C# is a fairly new language for me, so there might be some best practises that I may have missed. I am also wondering how this would behave in a multi-threaded environment.

Your AverageHalfSpreadIndicator doesn't implement IIndicator. I think you just forgot to

public class AverageHalfSpreadIndicator : IIndicator

You should instantiate your _lastUpdates:

public AverageHalfSpreadIndicator(IMarketDataFeed marketDataFeed)
{
    _lastUpdates = new List<MarketDataUpdate>();
    marketDataFeed.Update += OnMarketDataUpdate;
}

I don't like this code:

_lastUpdates.Add(updates);
if (_lastUpdates.Count == 20)
{
    _lastUpdates.RemoveAt(0);
}

I mean, it's fine, but I'd prefer to implement the _lastUpdates as a fixed sized queue. You can find many implementations on SO, for example: 1, 2, 3.

Also, your code is not thread-safe. If your code'd be called by many threads it'd break.

Don't use DateTime to measure execution time. There is a special class for this task: System.Diagnostics.Stopwatch.

var stopWatch = new Stopwatch();
stopWatch.Start();
var spreads = _lastUpdates.Select(x => 100 * (x.Mid - x.Bid) / x.Bid);
Console.WriteLine("Time to compute spreads: " + stopWatch.ElapsedMilliseconds + " ms");

var averageSpread = spreads.Average();
Console.WriteLine("Total time:" + stopWatch.ElapsedMilliseconds + " ms");
stopWatch.Stop();

This is not a big deal, but instead of encapsulating just Mid you can encapsulate whole 100 * (x.Mid - x.Bid) / x.Bidin your MarketDataUpdate.


Python Email Program I've written this email program in Python, and I would like to get some feedback on it. (i.e. closing the server twice after the raw input, etc.)

import console
import socket
import smtplib
from getpass import getpass

user = raw_input('What is your name? ').title()
host_name = socket.gethostname()
print '\nThis program is being run by %s on machine name %s' % (user, host_name)

# here we collect the login information for the email
# we also collect the email provider to automatically change the server
email_provider = raw_input('Gmail, AOL, Yahoo! or Comcast? ').title()
email_user = raw_input('Type in your full email username. ') # collect the user name
email_pwd = getpass('Type in your email password. ') # collect the users password

if email_provider in ('Gmail', 'Google'):
    smtpserver = smtplib.SMTP("smtp.gmail.com",587)
if email_provider in ('Aol', 'AOL'):
    smtpserver = smtplib.SMTP("smtp.aol.com",587)
if email_provider in ('Yahoo', 'Yahoo!'):
    smtpserver = smtplib.SMTP("smtp.mail.yahoo.com",587)
if email_provider in ('Comcast'):
    smtpserver = smtplib.SMTP("smtp.comcast.net",587)

smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(email_user, email_pwd)

def main():
    run_again = 'y'
    while run_again == 'y':
        # here we are collecting the Sendto email address
        # we save the input to a variable named sendto
        sendto = raw_input('Email address to send message to: ')
        to = sendto
        CC = sendto

        subj = raw_input('Subject: ')

        header = 'To: ' + to + '\n' + 'From: ' + email_user + '\n' + 'Subject: ' + subj +'\n'
        print '\nMessage Details:'
        print (header)

        assignment = raw_input('Message: ')

        msg = header + assignment + '\n'
        smtpserver.sendmail(email_user, to, msg)
        console.hud_alert('Your message has been sent!', 'success', 2.1)
        run_again = raw_input('Would you like to send another message? y or n')
        if run_again == 'n': smtpserver.close()
        else: smtpserver.close()

main()

I would suggest putting all the code in main() except for the imports. Then at the bottom, do

if __name__ == '__main__':
    main(sys.argv)

The advantage of structuring your file this way is that you can run the python interpreter and import your code and play with it. If you break it up into multiple routines, you can run each routine by itself to be sure it does the right thing. As it is now, if you try to import the file, half the code will be run immediately and then main() gets run unconditionally at the bottom, so you can't control it easily in the python interpreter.
To translate email_provider to smtp_server, you could do

host = {'GMAIL': "smtp.gmail.com",
        'GOOGLE': "smtp.gmail.com",
        'AOL': "smtp.aol.com",
        'YAHOO': "smtp.mail.yahoo.com",
        'COMCAST': "smtp.comcast.net"}

smtpserver = smtplib.SMTP(host[email_provider.strip('!').upper()], 587)

And you might want to handle the case where the user types an unknown email provider.

try:
    smtpserver = smtplib.SMTP(
          host[email_provider.strip('!').upper()], 587)
except KeyError:
    print("Unknown email provider '%s'" % email_provider)
    sys.exit(1)

Instead of making the user type 'y' to run again every time, you could do something like this:

while True:
    sendto = raw_input("Target address (or just hit ENTER to quit) > ")
    if sendto == '':
        sys.exit(0)
    to = CC = sendto
    ...


Playing Card Class - is this right? The Art and Science of Java, a course book that uses the ACM library, has an exercise that reads like this:

  Implement a new class called Card that includes the following entries:

  • Named constants for the four suits (CLUBS, DIAMONDS, HEARTS, SPADES)
  and the four ranks that are traditionally represented in words (ACE,
  JACK, QUEEN, KING). The values of the rank constants should be 1, 11,
  12, and 13, respectively.

  • A constructor that takes a rank and a suit and returns a Card with
  those values. • Accessor methods getRank and getSuit to retrieve the
  rank and suit components of a card.

  • An implementation of the toString method that returns the complete
  name of the card.

Now, this question is a little hard to understand, but nevertheless I've done a class that I think answers this:

/** Ficheiro: Carta.java
*
* Classe que vai representar os diferentes valores e naipes de uma carta de um baralho normal
* de 52 cartas. Exercício para construir uma classe completa juntamente com construtores,
* gets de Valores e Naipes e de método toString.  */

public class Carta {

// Constantes do Naipe (Rank) das Cartas
public static final String AS = "Ás";
public static final String REI = "Rei";
public static final String DAMA = "Dama";
public static final String VALETE = "Valete";

// Constantes do Valor Facial (Suit) das Cartas
public static final String COPAS = "Copas";
public static final String PAUS = "Paus";
public static final String OUROS = "Ouros";
public static final String ESPADAS = "Espadas";

/**
* Cria uma nova carta de um baralho regular de 52 cartas.
* @param valor O valor facial da carta (ex. Ás, Rei, 10...)
* @param naipe O naipe da carta
*/

public Carta (int valor, int naipe) {
    valorCarta = valor;
    numNaipe = naipe;
}

/**
 * Obtém o valor facial da carta (existem 13 valores faciais)
 * @return O valor facial da carta em String
 */

public String getValorCarta() {
    switch (valorCarta) {
    case 1: return AS;
    case 11: return REI;
    case 12: return DAMA;
    case 13: return VALETE;
    default: return "" + valorCarta;
    }
}

/**
 * Obtém o naipe da carta (existem 4 naipes)
 * @return O naipe da carta em String
 */

public String getNaipeCarta () {
    switch (numNaipe) {
    case 1: return COPAS;
    case 2: return PAUS;
    case 3: return OUROS;
    case 4: return ESPADAS;
    default: return ("Naipe Inválido");
    }
}

/**
 * Cria uma representação em String da Carta (Valor e Naipe)
 * @return Representação em String da Carta (Valor e Naipe)
 */

public String toString () {
    return getValorCarta() + " de " + getNaipeCarta();
}

// variáveis de instância que registam o Valor e o Naipe da Carta em integers.
private int valorCarta;
private int numNaipe;
}

Sorry about the commentaries.  I speak Portuguese and I've written the commentaries in that language. I hope the code is easy enough to understand.

I want to know if I have written well the class, according to specifications, especially the Public Constants and the Get Methods.

I am not sure if they wanted me to make the constants int types instead of Strings. Also I didn't assign any parameters to the Get Methods, so they will only give me the rank or suit of a new Card Object. I am not sure if they wanted me to attribute an int param to these methods.

As a sidenote, I wrote a short program that demonstrates this class in action, in choosing and displaying a random card (note that the run method is the main method in acm libraries):

import acm.program.*;
import acm.util.*;

public class testeCarta extends ConsoleProgram{

// Escolhe uma carta aleatóriade um baralho de 52 cartas e imprime no ecrã
public void run () {
    println ("Este programa vai seleccionar uma carta aleatória");
    Carta cartaAleat = new Carta (rGen.nextInt(1,13),rGen.nextInt(1,4));
    println (cartaAleat);
}

// variável de instància que permite a utilização de gerador de números aleatórios
public RandomGenerator rGen = RandomGenerator.getInstance();
}

Enums

Java enums have been around for a long time, so it's really a waste not to be using them at this point.

However, I understand that this may simply be an exercise, to test and build your understanding of Java, one step at a time.  If that is the case, (or maybe if the exercises are given in a specific order, and you haven't yet progressed to the Chapter on enums) then maybe you are intended to avoid them.  I'll work on that assumption (no enums).

Representation

  Named constants for the four suits (CLUBS, DIAMONDS, HEARTS, SPADES)
  and the four ranks that are traditionally represented in words (ACE,
  JACK, QUEEN, KING).

A named constant simply means that in code, your Java can refer to constant values with variable names that are meaningful.  It does not mean that those constants must be Strings, and usually, they should not be.  If you have:

public class Carta {

    public static final int REI = 11;

then, this code is using a named constant:

int rank = REI;

while this code is not:

int rank = 11;

  The values of the rank constants should be 1, 11, 12, and 13, respectively.

This clearly is requesting an implementation where the constants are integer types, not String objects:

public static final int AS = 1;      // Ace
public static final int VALETE = 11; // Jack
public static final int DAMA = 12;   // Queen
public static final int REI = 13;    // King

(you'll have to double check my values .. I don't speak Portuguese, and I think I recognize the word Rei and Dama, but not the others)

These constants are in addition to the other cards, ranked 2 through 10.

Specification Terminology

  ... that are traditionally represented in words

This language may be confusing, I understand.  When they say "words", they're not asking for String constants.  They are referring to the fact that, outside of JACK/QUEEN/KING/ACE, the other cards are 2,3,4,5,6,7,8,9,10 ... which are numbers.

Comments

You have some comments here (again, with my limited understanding of Portuguese), that I believe are backwards:

// Constantes do Naipe (Rank) das Cartas
public static final String AS = "Ás";

// Constantes do Valor Facial (Suit) das Cartas
public static final String COPAS = "Copas";

As (or Ace) is not a Naipe, it is a Valor (value, or rank).  Even if the code is correct, it's important in software development to make the comments correct, too.

toString()

Finally, concerning the specification for toString(), I believe that your existing code satisfies the requirement.  But, remember, the constants really should be implemented as integers, not String constants.  So, if you rewrite the class to represent values as integers, you'll then need a new method to convert integer values to Strings.

A HashMap<Integer,String> would probably work for that purpose.  If you haven't gotten to using generics yet, even an array of strings (String[]) could be used to map int values to String, as long as you keep track of the 0 index, or use the 0 index element as an Invalid value.


PHP Authentication Class I could use some review on this Auth class, as I'm sure it could use many improvements!

I wrote this fairly quickly, so please forgive any overseen bugs.

Example usage:

// Login a user
if (Auth::attempt($_POST['username'], $_POST['password']))
{
    echo 'You have successfully logged in.';
}

// Check if the user is a guest
if (Auth::guest())
{
    echo 'Please log in to see this page.';
}

// Logout a user
Auth::logout();

Source code:

<?php

class Auth {

    /**
     * Whether or not the user is currently logged in
     *
     * @var bool
     */
    public static $user;

    /**
     * Attempts to log a user in with the given credentials
     *
     * @param   string  $username
     * @param   string  $password
     * @return  bool
     */
    public static function attempt($username, $password)
    {
        global $db;

        $stmt = $db->prepare('SELECT * FROM `users` WHERE `username` = :username AND `password` = :password');
        $stmt->execute(array(':username' => $username, ':password' => $password));

        if ($user = $stmt->fetch(PDO::FETCH_ASSOC))
        {
            static::$user = $user;

            static::login($user['id']);

            return true;
        }

        return false;
    }

    /**
     * Login the user with the given id
     *
     * @param   int     $user_id
     * @return  void
     */
    public static function login($user_id)
    {
        if ($user_id)
        {
            $_SESSION['user_id'] = $user_id;
        }
    }

    /**
     * Log out the current user
     *
     * @return  void
     */
    public static function logout()
    {
        static::$user = null;

        $_SESSION = array();
        unset($_SESSION['user_id']);
        session_destroy();
    }

    /**
     * Checks if a user is currently logged in
     *
     * @return bool
     */
    public static function check()
    {
        return isset($_SESSION['user_id']);
    }

    /**
     * Checks if the current user is a guest
     *
     * @return  bool
     */
    public static function guest()
    {
        return ! isset($_SESSION['user_id']);
    }

}

I'm also not sure if using all static methods in the class could lead to problems later on?

I know there is currently no password hashing - I'm still working on the hashing method.

What's up with static?
Your class is build like a utility class:

UtilityClasses are classes that have only static methods that perform some operation on the objects passed as parameters. Such classes typically have no state.

but it's not really one as it has state, you have a $user property you need to pass around, and your functions depend on it. You are writing procedural code using classes, and there really isn't any reason for that, if procedural programming feels more natural with you, go with it, object orientation is not the one true paradigm. PHP supports both paradigms equally, and they are both equally valid.
The only benefit of using static like this is namespacing your functions, but that's not a real benefit as PHP supports namespaces. You should either write your class as an object oriented class, or a set of namespaced utility functions, everything in between is just bad form.
Since you mentioned execution speed, yes calling a static function is faster than instantiating an object and then calling the function, but the difference is in the range of a fraction of a millisecond. A more convincing argument for using static would be memory use, not execution speed, as the instantiated object will take up some memory. Still the argument is moot as there's absolutely no reason to write procedural code with classes, other than perhaps a misguided notion that everything needs to be object oriented.
Public properties
public static $user;

Assuming you rewrite this to be an object oriented class you should avoid public properties, or you'd be breaking encapsulation. All your class properties should be private and accessible through getters and setters, you should even avoid protected if you are not going to extend the class.
Global
global $db;

Don't. Please forget the global keyword even exists, and pass your dependencies through method parameters:
public static function attempt(PDO $db, $username, $password) {
    ...
}

Note the added benefit of type hinting. If you need that PDO object anywhere else in your class, then you should really transform this into an object oriented class and feed the dependency through its constructor:
class Auth {

    private $pdo;

    public function __construct(PDO $pdo) {
        $this->setPDO($pdo);
    }

    public function setPDO(PDO $pdo) {
        $this->pdo = $pdo;

        return $this;
    }

    public function getPDO() {
        return $this->pdo;
    }

    ...

}

Auth::setPDO() and Auth::getPDO() are mutator methods, keeping the class in line with encapsulation and any half decent IDE can generate them automatically for you. Notice that I return $this; in the setter? That's because I like chaining my methods, up to you if you want to follow my style.
Session
$_SESSION is also a global, and suffers from everything global variables do, but given that it's a native (almost) always on global, we can live with it. Obviously your class would be useless if at some point you decide to store session information in a different storage, but let's worry about than when and if it happens. However you need to keep in mind that $_SESSION is an external dependency, and your class needs to compensate for the rare occasion that you forgot to call session_start(). For example this:
public static function login($user_id)
{
    if ($user_id)
    {
        $_SESSION['user_id'] = $user_id;
    }
}

will fail. Either call session_start() before your class definition or in the constructor. In either case, do check if $_SESSION already exists before calling it.
Further reading

Why is Global State so Evil?
SOLID - Don't cheat, read all the linked articles.
Dependency inversion principle
Dependency injection
Inversion of Control Containers and the Dependency Injection pattern
When to Use Static Classes in C# - Not really C# specific, at least not the top voted answer.


Should I differentiate object types by calling methods via variables? I have three JavaScript objects. The first one serves merely as a prototype. The other two are implementation of a specific type.

var MainPrototype = {};
var SpecificType = $.extend(Object.create(MainPrototype));
var OtherSpecificType = $.extend(Object.create(MainPrototype));

I am missing the option of a parent-method and am trying to implement some specifics for each specific type.

I started of by comparing the types within the main prototype object, yet I found the nesting if-else clausing worrysome and in case that new types would be introduced, I guess the code will get ugly fast. I also does not feel right to put the specifics into the main function.

var MainPrototype = {
    function: bind() {
        //generic stuff

        if (this.type === SpecificType.type) {
          // specifics for this type
        } else if (this.type === OtherSpecificType.type) {
          // specifics for other type
        }
    }
}

That's why I thought I could call a specific function dynamically.

var MainPrototype = {
    bind: function() {
        //generic stuff
        var specificBindMethod = this.type + "Bind";
        if (typeof(this[bindMethod]) === "function") {
            this[categoryBindMethod]();
        }
    }
};

var SpecificType = $.extend(Object.create(MainPrototype, {
  type: 'specific',
  specificBind: function() {
    // doSpecifics
  }
});
var OtherSpecificType = $.extend(Object.create(MainPrototype, {
  function: 'other',
  otherBind: function() {
    // doSpecifics
  }
})

Now I can define the specific function within its proper object and only if they exist they will get called getting rid of the nesting.

I am wondering if this approach is better in terms of maintainability and extensibility or if I should have stayed with the if-else-approach or if there is anything better to solve my current use case.

Why wouldn't you create a method with a unique name accross all subclasses? In this way you don't have to generically build a function name, which can be a pain to maintain code / refactor

For instance :

var MainPrototype = {
    bind: function() {
        //generic stuff
        if (typeof(this.subBind) === "function") {
            this.subBind();
        }
    }
};

var SpecificType = $.extend(Object.create(MainPrototype, {
    type: 'specific',
    subBind: function() {
        // doSpecifics
    }
});
var OtherSpecificType = $.extend(Object.create(MainPrototype, {
    function: 'other',
    subBind: function() {
        // doSpecifics
    }
})


Speeding up Parallel.ForEach iterating through datatable and rendering report I am attempting to speed up the processing of 5000 rows received from the database, which I then render as a crystal report, export to a byte stream and save in a database table.

Currently I am using parallel.ForEach on a datatable. This uses 40 parallel processes that then sequentially iterates 125 (i.e 5000/40) records each of the 5000 rows.

It take approximately 5 minutes to do 5000 records at the moment. Is there any way I can speed this up?

DataTable dtBills = new DataTable("dtBills");//I used an OdbcDataAdapter to fill this with 5000 records

private void ParallelProcess()
{
    int numbills = 5000;
    int numprocesses = 40;
    int numsequential = numbills / numprocesses;

    int p=0;

    if (numbills < numprocesses)
        p = numbills;
    else
        p = numprocesses;

    Parallel.For(1, p+1, new ParallelOptions { MaxDegreeOfParallelism = 40 }, i =>
        {
            SequentialProcess(numsequential,i);

        });

}

private void SequentialProcess(int batch,int num)
{
    ReportDocument cryRpt = new ReportDocument();
    cryRpt.Load(Application.StartupPath + "\\" + "Bill.rpt");

    foreach(DataRow drow in dtBills.Rows.Cast<System.Data.DataRow>().Skip((num - 1) * batch).Take(batch).CopyToDataTable().Rows)

    {

        cryRpt.SetParameterValue(..... //here I set Crystal report Parameter values

        Stream stream = cryRpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
        byte[] contents = new byte[stream.Length];
        stream.Read(contents, 0, (int)stream.Length);
        stream.Close();

        if (!string.IsNullOrEmpty(sconnstr))
        {
            using (SqlConnection sconn = new SqlConnection(sconnstr))
            {

                String qry = @"INSERT INTO ....."
                using (SqlCommand cmd = new SqlCommand(qry, sconn))
                {

                    cmd.CommandType = CommandType.Text;

                    cmd.Parameters.AddWithValue(// Set values for insert here one of which will be contents

                    cmd.Connection = sconn;

                    sconn.Open();
                    cmd.ExecuteNonQuery();
                    sconn.Close();

                }

            }
        }

    }

    cryRpt.Close();
    cryRpt.Dispose();

}

The first thing here, you shouldn't be using directly the DataTable to read the results, use the method CreateDataReaderto create a Data Reader from it.

The difference is that a DataTable will check back on the database server on each iteration, while the DataReader will get a snapshot of your data and not check back anymore. That saves lots of time when dealing with a big number of records.

In this same channel, don't make the INSERT directly, but create it on memory, like an array or something (I think there is too something in the DataBase class that manages it in memory and commits later, but don't remember its name). If you stop dealing with the database one-at-a-time and doing in chunks, the speed of your code will improve.


Check if a record already exists and update if it exists I want to check if a record already exists in a database, and return true or false.

If it exists, I want to update the update_at datetime field.  The method touch_with_version from the paper_trail gem will do it.

Which is better?

  def repeated?(unprocessed)
    occurrence = Occurrence.find_by(unprocessed: unprocessed)
    occurrence.touch_with_version if occurrence.present?
    occurrence.present?
  end

Or

  def repeated?(unprocessed)
    occurrence = Occurrence.find_by(unprocessed: unprocessed)

    if occurrence.present?
      occurrence.touch_with_version
      true
    else
      false
    end
  end

Is there a better way?

When working within a Rails environment you can use try

occurrence.try(:touch_with_version)
occurrence.present?

Ruby also provides tap so you could also write:

occurrence.tap({|o| o.try(:touch_with_version)}).present?

You could even dig into the libraries to see that touch_with_version returns the result of ActiveRecord's save! which in turn returns the result of create_or_update which returns a boolean and so you can write:

occurrence.try(:touch_with_version).present?

If you're happy returning true, false, or nil you could shorten it to:

occurrence.try(:touch_with_version)

Very nice but also more fragile if either of those libraries decide to change their return values in a future release.


Compare two sheets and copy new and delete duplicates in excel I have two sheets  named source and solution and I have to perform following activities.

Copy new records from source to solution.
Update records if rows present in source and have updated data.
Delete rows from solutions if rows not present in source file.

I have written following vba code.

Sub processDuplicate()
    Application.ScreenUpdating = False
    'Declare variables
    Dim Sample As String, test As String
    Dim var As Variant, iRow As Long, iRowL As Long, RowsRange As String
    Dim Row As Long, FoundDup As Range, SalesSampleOrderClmn As Variant
    test = "test"
    Sample = "Sample"

    Dim CountTempNoDup As Integer
    Dim CountTempDup As Integer

    SalesSampleOrderClmn = Application.Match("Sales", Sheets(Sample).Rows(1), 0)
    Debug.Print SalesSampleOrderClmn

       'Set up the count as the number of filled rows in the first column of Sheet1.
       iRowL = Worksheets(Sample).Cells(Rows.count, SalesSampleOrderClmn).End(xlUp).Row
        Debug.Print iRowL
       'Cycle through all the cells in that column:
       For iRow = 2 To iRowL
       'Call subRemoveNotRequiredRows(TempData, SaleSamples)
       '(CompareFrom As String, DeleteFrom As String)
          Set FoundDup = Worksheets(test).Range("D:D").Find(Cells(iRow, SalesSampleOrderClmn), LookIn:=xlValues, lookat:=xlWhole)

        If FoundDup Is Nothing Then
            CountTempNoDup = CountTempNoDup + 1
            Worksheets(Sample).Cells(Row, ColumnNumber).EntireRow.Delete
        Else:
           CountTempDup = CountTempDup + 1
           RowsRange = "A" & iRow & ":M" & iRow
             Worksheets(test).Range(RowsRange).Copy Worksheets(Sample).Range(RowsRange)
             Worksheets(test).Cells(iRow, SalesSampleOrderClmn).EntireRow.Delete
        End If
       Next iRow
       Debug.Print CountTempNoDup; "No Dup"
       Debug.Print CountTempDup; "Dup"
    'Application.ScreenUpdating = True
    Worksheets(test).Rows(1).EntireRow.Delete
    Worksheets(test).UsedRange.Copy
 'Sheets(Test).Copy After:=Worksheets(Sample).Cells(Rows.count, 1).End(xlUp)
    Worksheets(Sample).Cells(Rows.count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
    Application.CutCopyMode = False
    Worksheets(test).Delete
End Sub

I'm facing performance issues in excel and I need help to increase performance and its correctness.

I have one problem with deleting entire row. It's not deleting entire row on till column L.

Worksheets(Sample).Cells(Row, ColumnNumber).EntireRow.Delete

I think you should be having performance issues because you have this:

   'Cycle through all the cells in that column:
   For iRow = 2 To iRowL
   'Call subRemoveNotRequiredR...

So you're going top to bottom and every time you go down and delete a row everything needs to shift up and recalculate and whatever. Go bottom up:

Print iRowL
       'Cycle through all the cells in that column:
       For iRow = iRowL to 2 step -1
       'Call subRemoveNotRequiredRows(TempData, SaleSamples)


Is this the right way to implement a hashmap? Is this the right way? Any comments on the hash functions? If asked a question like this in an interview, do I need to follow all OOP concepts(because I am using Java)? Like encapsulating the variables by writing getter and setter functions?

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class HashMapY<K,V> {
    private static int capacity = 100;
    private Map[] arr;
    private int size;

    public HashMapY () {
        arr = (Map[]) Array.newInstance(Map.class, capacity);
        size = 0;
    }

    public V get(Object key) {
        assert key!=null;
        int i = -1;
        if (key instanceof String || key instanceof Character){
            i = hash(key.toString(), capacity) % capacity;
        }
        if (key instanceof Integer)
            i = hash(Integer.parseInt(key.toString()))%capacity;
        while (!key.equals(arr[i].k) && i < capacity)
            i = (i+1)%capacity;
        return arr[i]==null ? null : arr[i].v;
    }

    public int hash(String value, int maxValue) {
        long hash = doHash(value);
        return (int) Math.abs(hash % maxValue);
    }

    private int hash(int k) {
        k *= 357913941;
        k ^= k << 24;
        k += ~357913941;
        k ^= k >> 31;
        k ^= k << 31;
        return k;
    }

    private long doHash(String str) {
        long hash = 5381;

        for (int i = 0; i < str.length(); i++) {
            hash = ((hash << 5) + hash) + str.charAt(i);
        }

        return hash;
    }
    public boolean isEmpty() {
        return size==0;
    }

    public void put(K key, V value) {
        assert key != null;
        int i=-1;
        if (key instanceof String || key instanceof Character){
            i = hash(key.toString(), capacity) % capacity;
        }
        if (key instanceof Integer)
            i = hash(Integer.parseInt(key.toString()))%capacity;
        while (arr[i]!=null) {
            i = (i+1);
        }
        if(arr[i] == null) {
            size++;
        }
        arr[i] = new Map(key, value);
    }

    public V remove(Object key) {
        List<Map> pairs = new ArrayList<Map>();
        assert key!=null;
        int i = -1;
        if (key instanceof String || key instanceof Character)
            i = hash(key.toString(), capacity) % capacity;
        if (key instanceof Integer)
            i = hash(Integer.parseInt(key.toString()))%capacity;
        while (!arr[i].k.equals(key) && capacity > i){
            i = i + 1;
        }
        if (capacity >= i){
            Object temp = arr[i].v;
            while (arr[i]!= null && arr[i].k.equals(key)){

                pairs.add(arr[i]);
                arr[i] = null;
                size--;
            }
            for (Map m : pairs){
                this.put(m.k, m.v);
            }
            return (V) temp;
        }
        return null;
    }

    public int size() {
        return size;
    }

    class Map {
        K k;
        V v;

        public Map(K key, V val){
            k = key;
            v = val;
        }
    }

    public boolean containsKey(K key) {
        assert key!=null;
        int i = -1;
        if (key instanceof String || key instanceof Character)
            i = hash(key.toString(), capacity) % capacity;
        if (key instanceof Integer)
            i = hash(Integer.parseInt(key.toString()))%capacity;
        if (arr[i] == null) return false;
        while (i<capacity && !arr[i].k.equals(key)){
            i = i+1;
        }
        return i<capacity;
    }

    public Set<K> keySet() {
        Set<K> set = new HashSet<K>();
        for (Map m : arr){
            if (m != null){
                set.add(m.k);
            }
        }
        return set;
    }

}

No, this is not the correct way to implement a hash map in Java, and especially not to implement the java.util.Map interface.

You aren't even explicitly implementing the Map interface.
What is (Map[]) Array.newInstance(Map.class, capacity) supposed to be? I'd think a simple new Map[capacity] would be sufficient.
You calculate your hashes yourself. However, every Object has a hashCode() method – use that instead.
Because you shouldn't calculate the hash code yourself, you shouldn't do any special handling of String and Character or Integer.
In your get method, what happens when the key is not one of your special classes? Not only will you be doing a linear search through your bins which breaks my expectations about the algorithmic complexity of a hash map, you'll also start at index -1, which will greet you with an ArrayIndexOutOfBoundsException.
Should you be implementing java.util.Map, the get method should throw an explicit NullPointerException rather than an optional assertion error as you do not want to allow null keys.
For some reason, the hash method is public. Furthermore, hash and doHash are not static although they don't access any object state.
Many criticisms about get also apply to put.
Your remove method sports this code: arr[i].k.equals(key). That risks a null pointer exception when arr[i] is null. Instead: arr[i] != null && key.equals(arr[i].k)
remove also has a loop that will only execute once or nonce:

while (arr[i]!= null && arr[i].k.equals(key)){
    pairs.add(arr[i]);
    arr[i] = null;
    size--;
}

Note that you never modify i.
In remove, it would be much simpler to look up the bin for that key, and then empty that bin rather. Consider putting your main get logic into a private int getBin(Object key), which you could use both in get:

int i = getBin(key);
... // assert i in range
if (arr[i] != null) {
    return arr[i].v;
}

and remove:

int i = getBin(key);
... // assert i in range
if (arr[i] != null) {
    V value = arr[i].v;
    arr[i] = null;
    size--;
    return value;
}

The Map class would usually be named Entry. Note that the java.util.Map interface contains a nested interface java.util.Map.Entry which you'd also have to implement for compatibility.
containsKey could be implemented using a getBin helper as

int i = getBin(key);
... // assert i in range
return arr[i] != null;

Your hash map can only contain 100 elements. If it grows larger, you should reallocate the storage array. You are furthermore resolving hash collisions via “open addressing” (using the next free bucket). Alternatively, you could use linked lists in each bucket, which also simplifies growing the hash table.

Then, there are many style problems in your code.

Consider changing a few whiles to easier to read for loops.
i = i + 1 could be abbreviated to i += 1, but is most often written i++.
Always use braces around the blocks of an if:

if (key instanceof Integer) {
    i = hash(Integer.parseInt(key.toString())) % capacity;
}

Many of your casts could be prevented with the use of generics. In remove, you have

Object temp = arr[i].v;
...
return (V) temp;

Instead:

V temp = arr[i].v;
...
return temp;

Regarding the question: “do I need to follow all OOP concepts(because I am using Java)?” – the answer is yes, but not because you are using Java. Instead, you'll want to write solid OOP code simply because your are writing object-oriented code, the language does not matter. Applying best practices is vastly preferable to producing shoddy, almost-working, bug-ridden code.

What now?

Your hash map implementation does not hold up to a code review. But that's not a problem.

First, learn more about the topic: there is the Wikipedia article on hash tables, there is also the source code for java.util.HashMap (e.g. here). Compare what you've learned with your code, and try to understand your errors.
Along the way, you'll probably also get better at actual Java programming.
Then, try again. Start with a clean slate, and write another HashMap implementation. I'm sure you'll have improved by leaps and bounds. And please come back to have your new code reviewed :)


Integral implemented using a trapezoidal Riemann sum I tried to make this a general purpose integral function but I want to know if it's efficient and idiomatic Rust.

use std::mem;

/// calculates the signed area between the function f and the x axis from
/// x = a to b using a trapezoidal Riemann sum. precision is the number of
/// trapezoids to calculate
pub fn integral<F>(a: f64, b: f64, f: F, precision: u32) -> f64
    where F: Fn(f64) -> f64 {

    let mut a = a;
    let mut b = b;

    let mut sign = 1.0;

    if a > b {
        mem::swap(&mut a, &mut b);
        sign = -1.0;
    }

    let delta = (b - a).abs() / precision as f64;
    let mut result = 0.0;

    for trapezoid in 0..precision {
        let left_side = a + (delta * trapezoid as f64);
        let right_size = left_side + delta;

        result += 0.5 * (f(left_side) + f(right_size)) * delta;
    }

    result * sign
}

And here's a test case

fn f(x: f64) -> f64 {
    (3.0 * x * x) + (4.0 * x) + 7.0
}

fn main() {
    let a = integral(0.0, 11.5, f, 1000000);
    println!("{}", a); // expect approx 1865.88
}

Embrace even more static analysis tools, such as clippy. It provides warnings like:

warning: `if _ { .. } else { .. }` is an expression
  --> src/main.rs:12:5
   |
12 |       let mut sign = 1.0;
   |  _____^ starting here...
13 | |
14 | |     if a > b {
15 | |         mem::swap(&mut a, &mut b);
16 | |         sign = -1.0;
17 | |     }
   | |_____^ ...ending here
   |

You don't need to rebind a variable to make it mutable. you can just add mut in the argument list.
When a where clause is used, the { moves to the next line.
Why is abs used?  Isn't it guaranteed that b > a?
"side" vs "size" seems likely to cause confusion. Either make them the same or distinct.
Using map and sum avoids needing to make result mutable.
Consider removing all mutability; either by reassigning all the variables at once:

let (a, b, sign) = if a > b {
    (b, a, -1.0)
} else {
    (a, b, 1.0)
};

Or by adding a small shim that reverses the order and applies the negation; I like the latter solution  because it ties the argument reversal and final negation closer together.

/// calculates the signed area between the function f and the x axis from
/// x = a to b using a trapezoidal Riemann sum. precision is the number of
/// trapezoids to calculate
pub fn integral<F>(a: f64, b: f64, f: F, precision: u32) -> f64
    where F: Fn(f64) -> f64
{
    fn core<F>(a: f64, b: f64, f: F, precision: u32) -> f64
        where F: Fn(f64) -> f64
    {
        let delta = (b - a) / precision as f64;

        (0..precision).map(|trapezoid| {
            let left_side = a + (delta * trapezoid as f64);
            let right_size = left_side + delta;

            0.5 * (f(left_side) + f(right_size)) * delta
        }).sum()
    }

    if a > b {
        -core(b, a, f, precision)
    } else {
        core(a, b, f, precision)
    }
}


Extract principal components First, I am aware that this can be done in sklearn - I'm intentionally trying to do it myself.

I am trying to extract the eigenvectors from np.linalg.eig to form principal components. I am able to do it but I think there's a more elegant way. The part that is making it tricky is that, according to the documentation, the eigenvalues resulting from np.linalg.eig are not necessarily ordered.

To find the first principal component (and second and so on) I am sorting the eigenvalues, then finding their original indexes, then using that to extract the right eigenvectors. I am intentionally reinventing the wheel a bit up to the point where I find the eigenvalues and eigenvectors, but not afterward. If there's any easier way to get from e_vals, e_vecs = np.linalg.eig(cov_mat) to the principal components I'm interested.

import numpy as np

np.random.seed(0)
x = 10 * np.random.rand(100)
y = 0.75 * x + 2 * np.random.randn(100)

centered_x = x - np.mean(x)
centered_y = y - np.mean(y)

X = np.array(list(zip(centered_x, centered_y))).T

def covariance_matrix(X):
    # I am aware of np.cov - intentionally reinventing
    n = X.shape[1]
    return (X @ X.T) / (n-1)

cov_mat = covariance_matrix(X)

e_vals, e_vecs = np.linalg.eig(cov_mat)

# The part below seems inelegant - looking for improvement
sorted_vals = sorted(e_vals, reverse=True)

index = [sorted_vals.index(v) for v in e_vals]

i = np.argsort(index)

sorted_vecs = e_vecs[:,i]

pc1 = sorted_vecs[:, 0]
pc2 = sorted_vecs[:, 1]

There is a nice trick to get the eigenvalues and eigenvectors of the covariance matrix without ever forming the matrix itself. This can be done using the singular value  decomposition (SVD), as described in this post from Stats.SE. Not only is this more numerically stable, but the results are automatically sorted.

A python version might look like this:

def components(X):
    _, vals, vecs = np.linalg.svd(X - X.mean(axis=0), full_matrices=False)
    return vals**2/(len(X)-1), vecs

A few things to note:

As described in the linked post above, the data matrix is typically defined with dimensions as columns, i.e. the transpose of your X.
The principle values and components are typically sorted from largest to smallest, i.e. the reverse of yours.
The function above does not assume that X has been pre-centered.

So to get results comparable to yours, you would need to do:

vals, vecs = components(X.T)
e_vals, e_vecs = vals[::-1], vecs[::-1]


Linear Probing in Python About to get back into coding, so I implemented linear probing in Python. How did I do? Anything unnecessary? Am I using "global" right? I originally wanted to use an array with a fixed length, but couldn't get it to work.

list = ["x", "x", "x", "x", "x", "x", "x"]
state = 0

def main():
#tests
    linearProbing(1)
    linearProbing(1)
    linearProbing(1)
    print(list)

def linearProbing(x):
    global state
    global list
    c = x % len(list)
    while state != 1:
        if list[c] == "x":
            list[c] = x
            state = 1
        elif c == 0:
            c = len(list) - 1
        else:
            c -= 1
    state = 0

if __name__ == '__main__':
    main()

Am I using "global" right?

It would be better to not use global at all,
by wrapping this in a class,
with state and list as attributes,
so an instance could reference them with self.state, for example.

  How did I do?

state is used as a boolean.
So it should be a boolean value,
set to False instead of 0 and True instead of 1 values.

list is not a good name for a variable,
as it shadows a built-in with the same name.

A quick tip: you can write ["x", "x", "x", "x", "x", "x", "x"] simpler as ["x"] * 7.
In any case, duplicating "x" in multiple places (first when you initialize list, and then later in linearProbing), is not great.
It would be better to put the value "x" in a variable, say FREESPOT = "x",
and reuse that everywhere.


Making a minishell in C. How to improve error control with feoh and ferror? I've written this minishell but I'm not sure I'm making a correct control of the errors. Could you give me some advice?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#define LINE_LEN  50
#define MAX_PARTS  50

int main ()
{
char* token;
char str[LINE_LEN];
char* arr[MAX_PARTS];
int i,j;
bool go_on = true;

while (go_on == true){
    printf("Write a line:('quit' to end) \n $:");
    fgets(str, LINE_LEN, stdin);

    if (str==NULL) {
        goto errorfgets;
    } else {
        size_t l=strlen(str);
        if(l && str[l-1]=='\n')
            str[l-1]=0;

        i=0;
        /* split string into words*/
        token = strtok(str, " \t\r\n");
        while( token != NULL )
        {
            arr[i] = token;
            i++;
            token = strtok(NULL," \t\r\n");
        }

        fflush(stdin);

        /* check if the first word is quit*/
        if (strcmp(arr[0],"quit")==0)
        {
            printf("Goodbye\n");
            go_on = false;
        } else {

            for (j=0; j < i; j++){
            printf("pajaritos por aqui'%s'\n", arr[j]);
            }
        }
    }
}

return 0;
errorfgets:
    printf("fgets didn't work correctly");
    return -1;
}

I think I haven't really understood the point of your program but I'll try to give you a few hints :

The indentation is not really good. I am not sure if this is because of SE but I'd rather point it out just in case.
You'll hear everything and its opposite about goto. It surely has drawbacks but it can be quite useful to handle errors and free the ressources that are not required anymore. However, in your case, there's no need for goto so it's probably better to Keep It Simple and just use return.
I am not a big fan of a go_on variable if break can do the trick. Also, it makes stdbool pointless.
Things will be easier to understand if you declare local variables in the smallest possible scope. Depending on the version of C you are using, you can even declare j in your for statement :  for (int j=0; j < i; j++).
Because of return/break, you can now remove levels of indentation (again, this is purely personal preference).
You shouldn't repeat "quit" in multiple places. It will make it hard to change if it has to change in the future.
There is a risk that arr[i] = token; goes to far if you ever change a constant without changing the other. Let's make sure it does not go too far.

At the end, here what the code would be like :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LINE_LEN  50
#define MAX_PARTS  50

static const char quit[] = "quit";

int main ()
{
    char str[LINE_LEN];
    char* arr[MAX_PARTS];

    for (;;) {
        printf("Write a line:('%s' to end) \n $:", quit);
        fgets(str, LINE_LEN, stdin);

        if (str==NULL) {
            printf("fgets didn't work correctly");
            return -1;
        }
        size_t l = strlen(str);
        if (l && str[l-1]=='\n')
            str[l-1]=0;

        /* split string into words*/
        int i;
        char* token = strtok(str, " \t\r\n");
        for (i=0; i<MAX_PARTS && token != NULL; i++)
        {
            arr[i] = token;
            token = strtok(NULL," \t\r\n");
        }

        fflush(stdin);

        /* check if the first word is quit*/
        if (strcmp(arr[0], quit)==0)
        {
            printf("Goodbye\n");
            break;
        }
        for (int j=0; j < i; j++){
            printf("pajaritos por aqui '%s'\n", arr[j]);
        }
    }

    return 0;
}


Functional implementation of collecting objects into groups Trying to write more functional Javascript with the underscore library. Any thoughts on how to improve this to be more functional than imperative? Also, any way to do this without mapObject?

The first of each pair is a "bucket" and the second is a value, I'd like to iterate over the information and get a unique list of what is in each bucket.

var a = [
  ['A',1],
  ['A',1],
  ['A',1],
  ['A',2],
  ['B',1],
  ['B',2],
  ['B',2],
  ['B',4],
  ['C',6],
  ['D',5]
];

//result should be:
//  {
//     A: [1,2],
//     B: [1,2,4],
//     C: [6],
//     D: [5]
//  }

_.chain(a)
 .groupBy(function (pair) {
    var group = pair[0];
    return group;
 })
 .mapObject(function (val, key) {
    var results = _.chain(val)
                   .map(function (pair) {
                     return pair[1]
                   })
                   .uniq()
                   .value();
    return results;
 })
 .value();

Well you can avoid a few variable assignments by returning immediately,
also by using some details of the Underscore.js API, that is by
specifying the used indexes for groupBy/map directly.

_.chain(a)
 .groupBy(0)
 .mapObject(function (val, key) {
    return _.chain(val).map(1).uniq().value();
 })
 .value();

I doubt there's a good way to avoid mapObject, after all, that's what
it's for.

With 1.7 you could also write that even less verbose:

_.chain(a)
 .groupBy(0)
 .mapObject((val, key) => _.chain(val).map(1).uniq().value())
 .value();

Looks pretty functional to me.


Newtonsoft Json helper class I've written the following helper class to help serializing objects to and from Json.

I would appreciate some feed back on the style of the class.

using System;
using System.IO;
using Newtonsoft.Json;

namespace Helpers
{
    public static class Json
    {
        public static void SaveContractToJSON<T>(T contract, string filePath)
        {
            using (var fs = File.Open(filePath, FileMode.OpenOrCreate))
            {
                SerializeToStream(contract, fs);
            }
        }

        public static void SaveContractToJSON<T>(T contract, MemoryStream stream)
        {
            SerializeToStream(contract, stream);
        }

        private static void SerializeToStream<T>(T contract, Stream stream)
        {
            var serializer = new JsonSerializer();

            using (var sw = new StreamWriter(stream))
            using (var writer = new JsonTextWriter(sw))
            {
                serializer.Serialize(writer, contract);
            }
        }

        public static T LoadContractFromJSON<T>(string filePath)
        {
            try
            {
                using (var fileStream = new FileStream(filePath, FileMode.Open))
                {
                    return DeserializeFromStream<T>(fileStream);
                }
            }
            catch (Exception)
            {
                return default(T);
            }
        }

        public static T LoadContractFromJSON<T>(MemoryStream stream)
        {
            try
            {
                return DeserializeFromStream<T>(stream);
            }
            catch (Exception)
            {
                return default(T);
            }
        }

        private static T DeserializeFromStream<T>(Stream stream)
        {
            var serializer = new JsonSerializer();

            using (var sr = new StreamReader(stream))
            using (var jsonTextReader = new JsonTextReader(sr))
            {
                return serializer.Deserialize<T>(jsonTextReader);
            }
        }
    }
}

Your class name is Json yet your methods contain this acronym in all-uppercase: JSON. The naming conventions say: "Do capitalize only the first character of acronyms with three or more characters, except the first word of a camel-cased identifier."

fs isn't a clear variable name. Same for sw (why not use streamWriter?) and sr (streamReader). I'd even include writer.

Considering that you don't use var serializer = new JsonSerializer(); until later, why not delay this call until you need it?

I'm bothered by catch (Exception). Are you certain you don't want to know that something went wrong, and what exactly went wrong?


Making change using the smallest possible number of coins I have written code based on the Greedy Algorithm. The example can be found here.

  Problem: Make a change of a given amount using the smallest possible number of coins.

  Coins available are:

  dollars (100 cents)
  quarters (25 cents)
  dimes (10 cents)
  nickels (5 cents)
  pennies (1 cent)

This outputs an array of coins:

<?php

function makeChange($amount) {
    $c = array(100, 25, 10, 5, 1);
    $sol = array();
    $sum = 0;
    $i = 0;
    $ele = $c['0'];
    while ($sum != $amount) {
        $sum = $sum + $ele;
        if ($sum > $amount) {
            $sum = $sum - $ele;
            $i++;
            $ele = $c[$i];
            continue;
        }
        $sol[] = $ele;
    }
    return $sol;
}

$amount = 356;
$sol = makeChange($amount);
print_r($sol);
?>

Can this become more optimized?

The following is a bit of variation, exploiting that one does not need to subtract the same coin value one-by-one.

$c = array(100, 25, 10, 5, 1); // $coins
$n = array(  0,  0,  0, 0, 0); // Number of coins.
$i = 0;
while ($amount > 0) {
    $k = floor($amount / $c[$i]); // How many coins
    $n[$i] = $k;
    $amount -= $k * $c[$i];
    // Or shorter: $amount %= $c[$i];
    ++$i;
}

Also, I subtract from the original amount instead of adding to $sum.


Handle multiple menu click events in one Sub I have multiple menus that will change languages in one Sub that will handle multiple events of the menus click, It is working for me right now, I'm just wondering if there is better way to do it... I'm using the menus name to call the process for each menus.

    Private Shared Sub CheckMenuItem(ByVal mnu As ToolStripMenuItem, ByVal checked_item As ToolStripMenuItem)

    For Each menu_item As ToolStripMenuItem In mnu.DropDownItems.OfType(Of ToolStripMenuItem)()
        menu_item.Checked = (menu_item Is checked_item)
    Next
End Sub

    Private Sub mnuEnglish_Click(sender As System.Object, e As EventArgs) _
    Handles mnuEnglish.Click, mnuFrench.Click, mnuDutch.Click, _
    mnuGerman.Click,mnuCroatian.Click, mnuCzech.Click, mnuHungarian.Click, _
    mnuIndonesian.Click, mnuItalian.Click, mnuPolish.Click, mnuSlovak.Click, _
    mnuSpanish.Click, mnuSwedish.Click, mnuTurkish.Click, mnuVietnamese.Click

    Dim item As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
    CheckMenuItem(mnuLanguage, item)

    Select Case item.Name
        Case "mnuEnglish"
            MsgBox("english")
        Case "mnuFrench"
            MsgBox("French")
        Case Else
            MsgBox("Other Languages")
    End Select
    End Sub

See this Stack Overflow answer, where Cody Gray explains the syntax for the two ways of registering event handlers in vb.net:

  The first involves the use of the Handles keyword, which you append to the end of the event handler method's definition. [...] The second involves the explicit use of the AddHandler statement, just like += in C#.

This means instead of statically declaring all the handled events with a Handles keyword, you write code to add the handlers yourself:

Public Sub New()

    InitializeComponents()

    AddHandler mnuEnglish.Click, AdressOf mnuEnglish_Click
    AddHandler mnuFrench.Click, AdressOf mnuEnglish_Click
    '...

End Sub

Or even better, loop through the menu items in that menu object, and assing all Click events the AdressOf mnuEnglish_Click, so you won't have the opportunity to forget updating that piece of code when you add a new language / menu item in the designer:

Public Sub New()

    InitializeComponents()

    ForEach item As ToolStripMenuItem In mnu.DropDownItems.OfType(Of ToolStripMenuItem)()
        AddHandler item.Click, AdressOf mnuEnglish_Click
    Next

End Sub


Caching an integer - distinguishing no value from zero I would like to store an integer value in the application cache.

The GetOrSet method should always return an int, and never be null. If there is no cached value, then the getDataCallback function will provide the integer.

But I need the cache check to return a nullable int in order to distinguish no value from a 0 integer value.

I am looking for any faults in the code below, or possible improvements.

It probably looks very simple to others, but I find working with nullable ints quite confusing.

public int GetOrSet(string cacheKey, Func<int> getDataCallback)
{
    int? data = (int?)HttpContext.Current.Application[cacheKey]; // this should return null if there is no value stored in the cache
    if (! data.HasValue)
    {
        int intData = getDataCallback();
        HttpContext.Current.Application[cacheKey] = intData;
        return intData;
    }
    else
    {
        return data.Value;
    }
}

Update

Big thanks to both @Nkosi and @Flater for their help.

I marked @Flater's answer as correct because I had to chose one only. But I really hated having to do this as both answers were equally helpful and I used both their suggestions in the end.  I would encourage anybody reading this to check them both out.

Your current method is slightly violating SRP, it's both checking is a cache exists, and creating one if needed. I'd prefer to separate the two:

public int GetData(string cacheKey, Func<int> getDataCallback)
{
    int? cachedData = (int?)HttpContext.Current.Application[cacheKey];

    return cachedData.HasValue
                        ? cachedData.Value
                        : CreateCache(cacheKey, getDataCallback);
}

private int CreateCache(string cacheKey, Func<int> getDataCallback)
{
    int intData = getDataCallback();
    HttpContext.Current.Application[cacheKey] = intData;
    return intData;
}

The first method only checks the existing cache and, if missing, calls the second method, which will create a new cache.

The code could be refactored to slightly shorten it, but that would be at the cost of readability.
I renamed GetOrSet to GetData for clarity's sake. The method exists to give you the data, regardless of whether it's cached or created on the fly. Pedantically, you'd need to name it Get_or_SetAndThenGet for it to be an accurate description. I prefer GetData, it sticks to the important facts and remains concise.
Nkosi is also correct in the comments that you should ideally abstract this snippet away from using HttpContext, but that's not necessary for the question at hand.

Note that you could also stop using int? if you find it confusing:

public int GetData(string cacheKey, Func<int> getDataCallback)
{
    object cachedData = HttpContext.Current.Application[cacheKey];

    return cachedData != null
                        ? (int) cachedData
                        : CreateCache(cacheKey, getDataCallback);
}


Determine if two arrays are a rotated version of each other Following algorithm has time complexity \$O(N)\$ and space complexity \$O(1)\$.

Any suggestions to improve this algorithm or any alternate approach suggestions?

public static boolean isRotatedVersion(int[] a, int[] b){
        //Check length of arrays
        if(a.length != b.length)
            return false;

        //Declare two pointers. One for each array
        int aPointer = 0;
        int bPointer = 0;

        //This flag will prevent us walking through the 2nd array in a loop only once
        //Helps avoid going into infinite walk loop
        boolean isFirstTimeWalk = true;

        //This loop will run from 0 to n of array 1
        while(aPointer < a.length){

            //If elements at position aPointer and bPointer are equal then incr both pointer
            // to move to the element in both array
            if(a[aPointer] == b[bPointer]){
                aPointer++;
                if(bPointer < b.length-1)
                    bPointer++;
                else{ //If reached end of second array then it reset pointer to 0 to rotate
                    //Once rotated done mark is1stTimeWalked to false so avoid future loops
                    isFirstTimeWalk = false;
                    bPointer = 0;
                }
            }else if(isFirstTimeWalk && bPointer < b.length-1){
                //Move bPointer until a match is found
                bPointer++;
            }else {
                return false;
            }
        }
        return true;
    }
}

Bug

I ran your code with the following arrays and your function thought that the two arrays were rotated versions of each other but they were not:

public static void main(String [] args)
{
    int [] a = { 0, 1, 2, 3, 0 };
    int [] b = { 0, 1, 1, 2, 3 };
    System.out.println(isRotatedVersion(a, b));
}

Output:

  true

Achieving a correct \$O(n)\$ solution

Determining if an array is a rotated version of another array is equivalent to searching for the first array within the second array concatenated with itself.  For example, given:

a = { 0, 0, 1, 2, 0 }
b = { 0, 0, 0, 1, 2 }

You can search for pattern a within this array, which is b doubled:

{ 0, 0, 0, 1, 2, 0, 0, 0, 1, 2 }

To do this in \$O(n)\$ time, you just need to implement a linear time string searching algorithm.  I would suggest using the Knuth Morris Pratt algorithm, which is easy to understand and implement.


Calculator with four arithmetic operations #include <iostream>

using namespace std;

class Math
{
private:
    int answer;
public:
    /*void getAn()
    {
        cout << answer;
    }*/
    int add(int x, int y)
{
    cout << "Add two numbers:\n";
    cin >> x;
    cin >> y;
    answer = x + y;
    return answer;
}
    int sub(int x, int y)
{
    cout << "Subtract one number from the other:\n";
    cin >> x;
    cin >> y;
    answer = x - y;
    return answer;
}
    int multi(int x, int y)
{
    cout << "Multiply two numbers:\n";
    cin >> x;
    cin >> y;
    answer = x * y;
    return answer;
}
    int divi(int x, int y)
{
    cout << "Divide two numbers:\n";
    cin >> x;
    cin >> y;
    answer = x / y;
    return answer;
}
};

int main()
{
    int choice;
    int x,y;
    int learning = 1;
    Math doMath;
    cout << "This is a calculator!\n" << endl;
    while(learning==1)
    {
        //doMath.getAn();
        cout << endl;
        cout << "Menu:\n1) Addition\n2) Subtraction\n3) Multiplication\n4) Division\n5) Quit\n" << endl;
        cout << "Choose your option.\n";
        cin >> choice;
        if(choice==1)
        {
            cout << doMath.add(x,y) << endl;
        }else if(choice==2)
        {
            cout << doMath.sub(x,y) << endl;
        }else if(choice==3)
        {
            cout << doMath.multi(x,y) << endl;
        }else if(choice==4)
        {
            cout << doMath.divi(x,y) << endl;
        }else if(choice==5)
        {
            learning = 0;
        }else
        {
            cout << "THAT WASN'T A CHOICE, YOU LITTLE SHIT!!!" << endl;
            learning = 0;
        }
    }
    return 0;
}

Do not insult users

"THAT WASN'T A CHOICE, YOU LITTLE SHIT!!!" is absolutely not acceptable.

You are repeating yourself a lot, in programming you should avoid as much as possible code repetition as it leads to bugs and is not easily reusable.

Instead do this:

template<typename Function>
int do_operation(int x, int y,Function operation,string message) {
    cout << message
    cin >> x;
    cin >> y;
    answer = operation(x,y);
    return answer;
}

You can then do:

int add(int x,int y) {
    return do_operation(x, y, std::plus<int>(), "Add two numbers:\n");
}

I see many many if and elif, it is better to use a switch statement. You may even use an array but perhaps then it would be overkill.
learning is quite puzzling as a name. A more standard running is more intuitive.
//doMath.getAn(); and

/*void getAn()
{
    cout << answer;
}*/

are really puzzling. Either uncomment them or delete them because commented out code confuses the readers.
Use constants for your strings and put them at the top. You can modify them in a simpler and faster way.

static const string Menu = "Menu:\n1) Addition\n2) Subtraction\n3) Multiplication\n4) Division\n5) Quit\n"
static const string ErrorMessage = "Invalid choice, enter a valid number."

and then

cout << Menu << endl;
cout << "Choose your option.\n";

and

else {
    cout << ErrorMessage << endl;
    running = 0;
}

Do not use while(learning==1) because it is redundant and weird, use the simpler while(learning) (or even better while(running))
Do not use using namespace std;, if you are curious to know why, look here
Put the first brace on the same line of the function title and indent the function body.

int add(int x, int y) {
    cout << "Add two numbers:\n";
    cin >> x;
    cin >> y;
    answer = x + y;
    return answer;
}


Given a digit sequence, print the possible decodings of the given digit sequence Examples:

12 gives:
'AB' and 'L'

and

123 gives
'ABC', 'LC' and 'AW'

Here is my attempt:
import java.util.*;

public class decoding {

static int calls = 0;
static Map<Integer, String> codes  = new HashMap<Integer, String>();

private static void construct(){
codes.put(1, "A");
codes.put(2, "B");
codes.put(3, "C");
codes.put(4, "D");
codes.put(5, "E");
codes.put(6, "F");
codes.put(7, "G");
codes.put(8, "H");
codes.put(9, "I");
codes.put(10, "J");
codes.put(11, "K");
codes.put(12, "L");
codes.put(13, "M");
codes.put(14, "N");
codes.put(15, "O");
codes.put(16, "P");
codes.put(17, "Q");
codes.put(18, "R");
codes.put(19, "S");
codes.put(20, "T");
codes.put(21, "U");
codes.put(22, "V");
codes.put(23, "W");
codes.put(24, "X");
codes.put(25, "Y");
codes.put(26, "Z");
}

private static void decode(String str, String built){

    construct();

    int n = str.length();

    if (n == 0) {
        System.out.println(built);
        return;
    }

        // If you have 0's, then they don't have a corresponding singular letter. Break off the recursion.
        if (str.substring(0, 1).equals("0"))
            return;

        String x = codes.get(Integer.parseInt(str.substring(0, 1)));
        if (x == null)
            return;
        decode(str.substring(1), built+x);

        if (n > 1) {

            // If it's more than 26, it doesn't have a corresponding letter. Break off the recursion.
            if (Integer.parseInt(str.substring(0, 2)) > 26)
                return;

            String y = codes.get(Integer.parseInt(str.substring(0, 2)));

            decode(str.substring(2), built+y);
        }
    }

public static void main(String[] args) {
    decode(args[0], "");
}
}

I haven't done any memoization or dynamic programming in this. It's a rather crude solution with exponential run time I believe. Let me know what you think.

A few hints :

compile your code with all warning enabled (calls doesn't seem to be used, does it ?)
write your code in such a way that it can easily be tested. This includes taking a well-defined input and returning a well-defined value (instead of printing it).
write tests.

An example of dynamic programming approach (but I do think it will be better for you if you try to rewrite your solution with tests first, at least you'll be able to compare results and performance): asuming you have a string s "dddddD1D2" (a string composed of any digits then digit D1 then digit D2) and you know the number of combinations for all strings "" (empty string), "d", "dd", "ddd", ..., "ddddddD1", how to you know the number of combinations for string s ?

You have two different options :

either D2 is to be converted on its own (if D2 is positive). So if D2 is positive, you have as many combinations like this one as you have combinations for "ddddddD1" which is a known value.
either D2 is to be converted with previous digit D1. This is possible if 1 <= 10*D1 + D2 <= 26. If it is the case, you have as many combinations as the number of combinations for "dddddd" which is a known value.

Well, I gave you a solution assuming you had a solution. How do you get such a thing working ? You just need to iterate over the string and for each string position, you keep track of the number of combinations that can be generated using characters up to that point :

0 character : 1 combination
1 character : it has to be converted on its own. Reuse the solution for 0 characters.
2 characters : reuse the solution for 0 and 1 characters
n characters : reuse the solution for n-2 and n-1 characters.

This is a classic example of algorithm where we can define the number of elements with a property without having to enumerate them.

You can either save the two last results as you progress or you can keep track of the result for each index in an array. This is up to you.

Reusing your exemples and adding more :

nb("") = 1
nb("1") = nb("") = 1
nb("12") = nb("1") + nb("") = 1 + 1 = 2
nb("123") = nb("12") + nb("1") = 1 + 2 = 3
nb("1234") = nb("123") = 3 because 34 cannot be converted to a letter.

Disclaimer : I haven't tried any of this but I think it kind of makes sense. You'll see if it works as you write tests and compare with the results from your current code.