Is my login function secure ? how to improve it? Using cakePHP, I would love to have your point of view about this large source code, i am not sure if its enought secure. sha1() will be removed with another script.
I found this large script can be optimized but how ?

i have many times

$this->Session->setFlash("You hav to complete each fiedls", "error");

and

$this->Request->redirect(SITE . "users/account");

I wonder if I can optmize this also:

class UsersController extends Controller {
    function account($Req){
        if(isset($Req->post->login)){
            $login = addslashes($Req->post->login);
            $password = sha1(addslashes($Req->post->password));
            $pass_confirm = sha1(addslashes($Req->post->pass_confirm));
            $email = addslashes($Req->post->email);
            $signature = addslashes($Req->post->signature);

            if(empty($login) || empty($email)){
                $this->Session->setFlash("You hav to complete each fiedls", "error");
                $this->Request->redirect(SITE . "users/account");
            }
            elseif($pass_confirm != $password) {
                $this->Session->setFlash("You gave two differents password", "error");
                $Req->redirect(SITE . "users/account");
            }

            $this->loadModel("Users");

            $dispoLogin = $this->Users->findCount(array(
                "login" => $login
            ));
            if($dispoLogin === 0){
                $this->Session->setFlash("The login is already use by someone else", "error");
                $this->Request->redirect(SITE . "users/account");
            }

            $dispoEmail = $this->Users->findCount(array(
                "email" => $email
            ));
            if($dispoEmail === 0){
                $this->Session->setFlash("Email adress already use by someone else", "error");
                $this->Request->redirect(SITE . "users/account");
            }

            if(empty($password)){
                $q = $this->Users->findFirst(array(
                    "fields" => "password",
                    "conditions" => array(
                        "id" => $this->User->id
                    )
                ));
                $password = sha1($q->password);
            }

            $this->Users->save(array(
                "id" => $this->User->id,
                "login" => $login,
                "password" => $password,
                "email" => $email,
                "signature" => $signature
            ));
            $this->user->setData(array(
                "login" => $login,
                "password" => $password,
                "email" => $email,
                "signature" => $signature
            ));

            $this->Session->setFlash("Your profile page is updated");
            $this->Request->redirect(SITE);
        }
    }
}

I answered you on SO but I'll repeat it here too.

Please read the CakePHP Documentation, preferably from the start because you really are getting a lot wrong here.

There is no need to addslashes() everything, (or anything ever)
CakePHP has it's own AuthComponent, so no need to roll your own
It also has a validation engine, so no need to validate anything here
You're also passing some Request object to the method? I don't even want to ask...

This action should basically be about 6 lines long. TL;DR: Read the CakePHP Authentication docs, and start again.


Inheritance or DI in Presenter classes in MVP In MVP pattern we can use DI when our presenter classes need services as follows:

class APresenter
{
    Public Presenter(DataService dataService, PermissionService permissionService)
    {
        //
    }

    // can use services injected through parameter...
}

As my all most all the presenter classed need these services, I can use inheritance instead, as follows:

class BasePresenter()
{
    DataService DataService = new DataService();
    PermissionService PermissionService = new PermissionService();
}

class APresenter :BasePresenter
{
    public APresenter()
    {
        //
    }

    // can use services inherited...
}

NOTE: Here I've not shown the Models and Views injected through the constructor of presenter classes

Is there a difference in these methods when it comes to dependency?
What would be the most suitable option out of these two to be implemented in some what small size project which is having about 20 presenter classes?

I would prefer the first one with constructor parameters primarily because of two reasons.

First, it's easier to test, you are able to pass mocked or dummy services directly to the constructor.

  Good design is testable, and design that isn't testable is bad.

(Source Michael C. Feathers: Working Effectively with Legacy Code)

The second reason is that it seems to me that the second snippet abuses inheritance. I don't see any reason why anyone would declare their variables with BasePresenter type (so it's useless) and using a base class only for sharing two lines of code also smells.

The third reasons is that the second version is harder to read, readers have to go back and forth to the parent class if they find an usage of dataService or permissionService and want to figure out what it is.


Codewars: "Twice linear" is running too long I am doing one kata on codewars:

  Consider a sequence u where u is defined as follows:

  The number u(0) = 1 is the first one in u. For each x in u, then y = 2
  * x + 1 and z = 3 * x + 1 must be in u too. There are no other numbers in u. Ex: u = [1, 3, 4, 7, 9, 10, 13, 15, 19, 21, 22, 27, ...]

  1 gives 3 and 4, then 3 gives 7 and 10, 4 gives 9 and 13, then 7 gives
  15 and 22 and so on...

  Task: Given parameter n the function dbl_linear (or dblLinear...) returns the element u(n) of the ordered (with <) sequence u.

  Example: dbl_linear(10) should return 22

  Note: Focus attention on efficiency

So far I did this algorithm but it's too slow and it times out. I do not really know how to refactor it. Can anyone help me? Maybe some another concept how to approach to solve it?

function dblLinear(n) {
   let arrOfResult = [1];
   const set = new Set();
   set.add(1);
   let nextInvoker = 0;

    while (arrOfResult.length <= n) {
       let y = 2 * arrOfResult[nextInvoker] + 1;
       let z = 3 * arrOfResult[nextInvoker] + 1;

       set.add(y);
       set.add(z);

       arrOfResult = Array.from(set).sort((a, b) => a - b);

       nextInvoker++;
  }

   arrOfResult.forEach(element => {
       let y = 2 * element + 1;
       let z = 3 * element + 1;

       set.add(y);
       set.add(z);
   });

   const result = Array.from(set).sort((a, b) => a - b);

   return result[n];
}

As I understand it, the algorithm employed is as follows:

set is a set of numbers, all of which are in the sequence
arrOfResult is set in order
Invariant: the first nextInvoker + 1 elements of arrOfResult are complete: i.e. they form a prefix of the sequence with no gaps
We loop, extending set/arrOfResult by two elements each time until we have n elements, so the prefix is of length n/2 (plus or minus a couple: I haven't worked through the exact details)
We then form the closure under the two operations \$x \to 2x+1\$ and \$x \to 3x+1\$ and claim that the prefix of that closure contains the first n elements with no gaps.

The claim of that last point seems to me to need a comment showing why it's true.

But more importantly, the way in which the algorithm works seems to repeat a lot of work.

       arrOfResult = Array.from(set).sort((a, b) => a - b);

sorts the same elements as the previous time round the loop, plus two more, with a full \$\Omega(n \lg n)\$ sort.

An easy way to get an asymptotic improvement would be to use a suitable priority queue. A standard binary heap would probably be sufficient. It would also simplify the main method: just three variables (the heap, a counter, and the previous element in order to detect duplicates) and a single loop.

In fact, if you really optimise that loop it's not even necessary to fully implement the binary heap.

function dbl_linear(n) {
    var prev = 0,
        heap = [1];
    while (true) {
        if (heap[0] === prev) {
            // Standard heap pop: move last element to replace first and downheap
            heap[0] = heap.pop();
        }
        else {
            if (n-- === 0) return heap[0];

            // We want to pop x and then insert 2x+1 and 3x+1
            // Since 3x+1 is guaranteed to be larger than anything in the heap,
            // we can optimise its insertion. Then we can combine the pop and the
            // other insertion into one assignment and a downheap.
            prev = heap[0];
            heap.push(3 * prev + 1);
            heap[0] = 2 * prev + 1;
        }

        // Push heap[0] down until we restore the heap property
        downheap(heap);
    }
}

downheap not included.

The clever way of tackling a problem about a sequence of integers if you're allowed access to the Internet is to use the Online Encyclopedia of Integer Sequences to see what's known about it. Searching for the prefix given in the problem statement gives two results: the desired sequence with and without duplicates. Since you clearly interpret the (IMO ambiguous) problem statement as being without duplicates, look at the notes on the sequence without duplicates:

  ...
  a(n+1) = A007448(a(n)); giving also the record values of A007448 and their positions. - Reinhard Zumkeller, Jul 14 2010
  ...

A007448 is

  Knuth's sequence (or Knuth numbers): a(n+1) = 1 + min( 2*a(floor(n/2)), 3*a(floor(n/3)) )

I would be happy to leave it there, but since some commentators doubted my observation that this gave a simple and efficient solution, two implementations.

A naïve implementation (ignoring everything from Zumkeller's comment after the semicolon) would be

function dbl_linear(n) {
    var cache = {0 : 1},
        a = 1,
        knuth = function(n) {
            return cache[n] = cache[n] || 1 + Math.min(2 * knuth(Math.floor((n - 1) / 2)),
                                                       3 * knuth(Math.floor((n - 1) / 3)));
        }
    while (n--) a = knuth(a);
    return a;
}

But taking into account the observations made after the semicolon we can optimise quite heavily. This is slightly less simple (although a binary search is still simpler than a heap), but more efficient, and easily argued to run in \$O(n \lg n)\$ time:

function dbl_linear(n) {
    // a(n+1) = A007448(a(n)); giving also the record values of A007448 and their positions.
    // - Reinhard Zumkeller, Jul 14 2010

    // A007448(n+1) = 1 + min(2 * A007448(n/2), 3 * A007448(n/3))

    var A002977 = [1],
        i,
        ai = 1;

    function A007448(x) {
        // Find the smallest element of A002977 which is larger than x by binary chop
        // Invariant: A002977[lb] <= x < A002977[ub]
        // To ensure the invariant we need a special case
        if (x === 0) return 1;

        var lb = 0,
            ub = A002977.length - 1,
            mid;
        while (ub - lb > 1) {
            mid = lb + ((ub - lb) >> 1);
            if (A002977[mid] > x) ub = mid;
            else lb = mid;
        }
        return A002977[ub];
    }

    for (i = 1; i <= n; i++) {
        ai = 1 + Math.min(2 * A007448(Math.floor((ai - 1) / 2)),
                          3 * A007448(Math.floor((ai - 1) / 3)));
        A002977[i] = ai;
    }

    return ai;
}

Performance measured on tio.run using Spidermonkey. Times based on a single run, so take with a pinch of salt. They are at least illustrative.

n    | original code | sineemore's code | heap   | naïve Knuth | optimised Knuth
100  |        0.034s |           0.038s | 0.028s |      0.026s |          0.029s
10^3 |        0.185s |           0.031s | 0.028s |      0.032s |          0.031s
10^4 |       15.2s   |           0.147s | 0.031s |      0.042s |          0.040s
10^5 |       timeout |           8.3s   | 0.042s |      0.268s |          0.051s
10^6 |       timeout |          timeout | 0.195s |      4.4s   |          0.155s
10^7 |    not tested |       not tested | 2.4s   |    segfault |          1.9s


Letting unbanned users comment I am a beginner in python and I was wondering if the code for this program is nice and clean. The program works I was wondering how could i improve it. So this program asks for your 'username' and checks to see if your 'username' is in the 'banned users' list. If it is then it will 'log out' and won't let you comment. If your 'username' isn't in the 'banned uses' list it will let you comment and ask if you would like to let someone else to comment. If you enter 'yes' the 'while loop' will run again if there isn't then the program will print the username and comments. This is basically what the program does. How can I improve it?

# We define a function here.
def display_comments():
    """Displays User Input (comments)"""
    if user_comments:  # If the dictionary 'user_comments' has at least one key value pair then execute this block of code.
        print("\nTop Comments:")
        for username, comment in user_comments.items(): # Define a for loop for each key value pair in the dictionary 'user_comments'.
            print(f"\n\t{username}: {comment}.")  # Print each key value pair.

username_prompt = "\nUsername: "
comment_prompt = "Comment: "
continue_prompt = "\nWould you like to let another user comment (yes/no)? "

banned_users = ['pete', 'jack', 'ali', 'henry', 'jason', 'emily'] # List of banned users.
user_comments = {} # Empty dictionary that we will store user input later on in the program.

account_active = True  # Set up a flag

while account_active: # Run as long as the 'account_active' flag remains 'True'.
    username = input(username_prompt) # Ask for a username and store it in the variable 'username'.

    if username.lower() in banned_users: # Cross reference the username with the banned_users list. If the username is in the banned_users list then execute the following code.
        print(f"\nI'm sorry {username} but you are banned from using this site.") # Tell the user that they are banned.
        print("Deactivating...") # Simulate logging out.

        account_active = False # Set the flag to 'False'

    else: # Run this code only if the user is not in the 'banned_users' list.
        comment = input(comment_prompt) # Ask the user for their comment and store it in the variable 'comment'.

        user_comments[username] = comment # Add the username and comment (key-value pair) to the 'user_comments' dictionary.

        repeat = input(continue_prompt) # Ask the user if they want to repeat and store it in the variable 'repeat'.

        if repeat == 'no': # If the variable 'repeat' has the value 'no' then execute the following code, otherwise run the while loop again.
            account_active = False # Set the flag to 'False'

display_comments() # Call the 'display_comments()' function to print the key-value pairs in the dictionary.

The code is pretty clear in what it does, good work.

# We define a function here.
def ...
...
... = {} # Empty dictionary
...
... = False # Set the flag to 'False'

Many of these comments are not all that useful. I can see by looking at the code that the flag is set to false, or that a function has been defined. Usually you would comment with the reason why a piece of code exists. If you removed a comment from beside some code, could you still work out what the code does? If so the comment is probably not useful.

def display_comments():
    """Displays User Input (comments)"""
    if user_comments:  # If the dictionary 'user_comments' has at least one key value pair then execute this block of code.
        print("\nTop Comments:")
        for username, comment in user_comments.items(): # Define a for loop for each key value pair in the dictionary 'user_comments'.
            print(f"\n\t{username}: {comment}.")  # Print each key value pair.

This looks pretty good. It does what it says it does. Remove the inline comments and it is golden.

banned_users = ['pete', 'jack', ...
...
if username.lower() in banned_users:

Extracting banned users to a list is good. An issue might arise here if a with an uppercase letter is is added to the banned list. He won't be prevented from commenting! In general there are two ways to avoid this problem

Do a case-insenstive compare. In a Java this would be done with username.equalsIgnoreCase(banned_user)
Normalize both strings and then compare. This is the recommended method in Python.

So I would suggest making sure every banned user has been lowercased

banned_users = ['pete', 'jack', ...
banned_users = [banned_user.lower() for banned_user in banned_users]

If you believe you'll ever need to deal with international usernames casefold may be of interest. It will normalize strings more aggressively than lower will.

One result of lowercasing the names is that it cuts down on the number of available usernames. If Ben Dor and B. Endor both try to sign up as BenDor and BEndor respectively, only one can get the name. Is that OK? Will this ever be a problem?

repeat = input(continue_prompt)
if repeat == 'no':

This is one place I would lowercase as an answer of "NO" or "No" clearly indicate the user is done.

user_comments = {}
...
user_comments[username] = comment

As the comments are stored in a dictionary, every username will only have one comment, the latest one they've made. For instance if the chat was meant to be

A: where is the coal?
B: walk west for 2 mins then
B: north for 1 min

The comments appear as

A: where is the coal?
B: north for 1 min

Is that intended? If not you can change to use a list containing the username and comment instead

comments = []
...
comments.append((username, comment))

and display_comments looks nearly identical

for username, comment in comments:
    ...


Concurrent version of List.map in Ocaml I would like to write a pseudo version of List.map with the help of the Event and Thread modules:

open Thread
open Event

let rec tmap f ls =
    let c = new_channel () in match ls with
        | [] -> []
        | h::t -> let _ = create (fun c -> sync (send c (f h :: tmap f t))) c in
            sync (receive c)

Calling the func:

# tmap (fun a -> a + a) [1;2;3;4;5;6];;
- : int list = [2; 4; 6; 8; 10; 12]

How can I make it smarter? In my case, I'm creating a thread for every list element, so if I had 10k elements this would be bad. My guess it that I should split the list, but how is it usually done?

An alternative would be to use a thread pool:
A fixed number of thread is created and each thread pulls and execute a task

Note that threads (from the Thread module) do not run in parallel (they are time-sharing)


pattern matching, replacement and for loop optimization in R 0
down vote
favorite

I have two data frame loc_df and and city_df (city and country) now loc_df has 5 column but considering only 2 here (Organization.Location.1 and Organization.Location.2) with 35000 row and city_df has 2 column (city and country) with 1000 rows. Now I am taking one value from city cloumn and matching with organisation column using grepl (for text matching ) and for loop(for iteration). I also have to maintain a index that's why I am using for loop. But this is taking huge amount of time.

I am trying to replace each city, state, province name to their country name in organization columns.

Please help me to optimize this code. I am very new to R.

for(k in 1:2){
  if(k==1){

    for (i in 1:nrow(city_df)) {
      x1 <- paste(" ", city_df$City[i], sep = "")
      x2 <- paste(" ", city_df$City[i], " ", sep = "")
      x3 <- paste(city_df$City[i], " ", sep = "")
      # print(x1)

      for (j in 1:nrow(loc_df)) {
        #print(loc_df$Organization.Location.1[j])

        if (grepl(x1, loc_df$Organization.Location.1[j]) |
            grepl(x2, loc_df$Organization.Location.1[j]) |
            grepl(x3, loc_df$Organization.Location.1[j])) {
            loc_df$org_new1[j] <- city_df$Country[i]
          break

        }

      }
    }
  }
  if(k==2){

    for (i in 1:nrow(city_df)) {
      x1 <- paste(" ", city_df$City[i], sep = "")
      x2 <- paste(" ", city_df$City[i], " ", sep = "")
      x3 <- paste(city_df$City[i], " ", sep = "")

      for (j in 1:nrow(loc_df)) {

        if (grepl(x1, loc_df$Organization.Location.2[j]) |
            grepl(x2, loc_df$Organization.Location.2[j]) |
            grepl(x3, loc_df$Organization.Location.3[j])) {
            loc_df$org_new1[j] <- city_df$Country[i]
          break

        }

      }
    }
  }

}

this is sample data I have generated using dput of city_df

structure(list(City = c("zug", "canton of zug", "zimbabwe",
                                  "zigong  chengdu", "zhuhai  guangdong  china", "zaragoza  spain"), Country = c("switzerland",
                                                                                "switzerland", "zimbabwe", "china", "china", "spain"
                                  )), .Names = c("City", "Country"), row.names = c(NA, 6L), class = "data.frame")

sample of loc_df

structure(list(Organization.Location.1 = c("zug  switzerland",
"zug  canton of zug  switzerland", "zimbabwe", "zigong  chengdu  pr china",
"zhuhai  guangdong  china", "zaragoza  spain"), Organization.Location.2 = c("",
"san francisco bay area", "london  canada area", "beijing city  china",
"greater atlanta area", "paris area  france")), .Names = c("Organization.Location.1",
"Organization.Location.2"), row.names = c(NA, 6L), class = "data.frame")

You can try something like this:

# function for string preperation:
preperString <- function(x) {
  require(stringr)
  x <- str_to_lower(x)
  x <- str_trim(x)
  x
}

setDT(loc_df) # convert data.frames to data.table
setDT(city_df)

loc_df <- loc_df[, lapply(.SD, preperString)] # apply string preperation to all columns of loc_df
city_df[, City := preperString(City)]

loc_df <- merge(loc_df, city_df, by.x = 'Organization.Location.1',
                by.y = 'City', all.x = T, sort = F)
loc_df <- merge(loc_df, city_df, by.x = 'Organization.Location.2',
                by.y = 'City', all.x = T, sort = F)
loc_df
#    Organization.Location.2         Organization.Location.1 Country.x Country.y
# 1:                                        zug  switzerland        NA        NA
# 2:  san francisco bay area zug  canton of zug  switzerland        NA        NA
# 3:     london  canada area                        zimbabwe  zimbabwe        NA
# 4:     beijing city  china       zigong  chengdu  pr china        NA        NA
# 5:    greater atlanta area        zhuhai  guangdong  china     china        NA
# 6:      paris area  france                 zaragoza  spain     spain        NA

# and then you can write rule tu create org_new1, for example:
loc_df[, org_new1 := Country.x]
loc_df[is.na(org_new1), org_new1 := Country.y]
loc_df
#    Organization.Location.2         Organization.Location.1 Country.x Country.y org_new1
# 1:                                        zug  switzerland        NA        NA       NA
# 2:  san francisco bay area zug  canton of zug  switzerland        NA        NA       NA
# 3:     london  canada area                        zimbabwe  zimbabwe        NA zimbabwe
# 4:     beijing city  china       zigong  chengdu  pr china        NA        NA       NA
# 5:    greater atlanta area        zhuhai  guangdong  china     china        NA    china
# 6:      paris area  france                 zaragoza  spain     spain        NA    spain


Python script to create Android/iOS strings from a .csv My script takes a CSV file of strings and then creates an XML file for Android and a .strings file to be used for iOS. It separates each locale meaning that a file will be created for each language.

This has come in handy so far, making localising mobile apps easier. However, I am always seeking feedback so let me know what you think.

Input

The .csv is provided in the format: key, language1, language2

key,en,ru
welcome_message,hello,здравствуйте
thank_you_message,thank you,спасибо
goodbye_message,goodbye,До свидания

Output

XML - Android

en_strings.xml

<?xml version="1.0" ?>
<resources>
    <string name="welcome_message">hello</string>
    <string name="thank_you_message">thank you</string>
    <string name="goodbye_message">goodbye</string>
</resources>

ru_strings.xml

<?xml version="1.0" ?>
<resources>
    <string name="welcome_message">здравствуйте</string>
    <string name="thank_you_message">спасибо</string>
    <string name="goodbye_message">До свидания</string>
</resources>

.Strings - iOS

en.strings

/*  */
"welcome_message" = "hello";

/*  */
"thank_you_message" = "thank you";

/*  */
"goodbye_message" = "goodbye";

ru.strings

/*  */
"welcome_message" = "здравствуйте";

/*  */
"thank_you_message" = "спасибо";

/*  */
"goodbye_message" = "До свидания";

At the moment I've just hardcoded the origin Strings.csv file and the user defines the target directory, this will be useful when integrating it into automatic builds etc.

# -*- coding: utf-8 -*-
# Script takes a csv and creates strings for Android (.xml) and iOS (.Strings).
# csv in the format [key, language1, langauge2 ......]
# usage - Python converter.py [FILEPATH]

import sys, os, getopt, csv, xml.etree.ElementTree as ET
from xml.dom import minidom

def prettify(elem):
    rough_string = ET.tostring(elem, 'utf-8')
    reparsed = minidom.parseString(rough_string)
    return reparsed.toprettyxml(indent="\t")

# Read in output directory
try:
   sys.argv[1:]
   fileDestination = sys.argv[1]
except IndexError:
   print "Error: Please supply an output directory."
   print "Usage: converter.py [FILEPATH]"
   sys.exit()

# Create directory if it doesn't exists
if not os.path.exists(fileDestination):
    os.makedirs(fileDestination)

# Read from csv
f = open('Strings.csv')
csv_f = csv.reader(f)

# Determine the number of languages from the csv
line1 = csv_f.next()
numberOfLocales =  len(line1)

# Create strings for each language
for x in range(1, numberOfLocales):
    #Returns to the start of the csv and ignores the first line
    f.seek(0)
    csv_f.next()
    rowIndex = 0

    # Android xml
    resources = ET.Element("resources")

    # Create iOS strings file
    iOSFile = open(fileDestination+"/"+line1[x]+".Strings", "w+")

    for row in csv_f:
        ++rowIndex
        try:
            # Write string to xml
            ET.SubElement(resources, "string", name=row[0]).text = row[x].decode('utf-8')
            # Write string to iOS .Strings
            iOSFile.write("/*  */\n")
            iOSFile.write('"'+row[0]+'"'+ ' = ' + '"'+row[x]+'"' + ";\n")
            iOSFile.write("\n")
        except IndexError:
            f.seek(0)
            print "There is a problem with the csv file at row {}".format(rowIndex+1) + " with the language {}".format(line1[x])
            r = list(csv_f)
            print r[rowIndex]
            sys.exit()
    # Write to Android file
    androidFile = open(fileDestination+"/"+line1[x]+"_strings.xml", "w+")
    androidFile.write(prettify(resources).encode('utf-8'))

The Python style for naming is snake_case for functions and variables, and PascalCase for classes. If you have constant variables (values don't change), they should be in UPPER_SNAKE_CASE.

You also shouldn't be importing multiple modules on one line, like you've done here:

import sys, os, getopt, csv, xml.etree.ElementTree as ET

These imports should be separated onto separate lines, as per PEP8, Python's official style guide.

These comments at the very top of your file:

# Script takes a csv and creates strings for Android (.xml) and iOS (.Strings).
# csv in the format [key, language1, langauge2 ......]
# usage - Python converter.py [FILEPATH]

Should be converted into a docstring, like this:

"""
Script takes a csv and creates strings for Android (.xml) and iOS (.Strings).
csv in the format [key, language1, langauge2 ......]
usage - Python converter.py [FILEPATH]
"""

This is not how you should be opening files.

f = open('Strings.csv')

Rather, you should use a with ... as ... context manager. By using a context manager, you can ensure that the file is properly closed. Here's how you'd do that:

with open("Strings.csv") as f:
    ...

Finally, the below line does nothing.

++rowIndex

It should be changed to row_index += 1, if I understand your intent correctly.


String pattern printing challenge Write a function that will take an integer argument say n.
n ranges from 2 <= n <= 100

The function would not take any argument from the user.
the function should print the pattern according to the value of n.

Let's say if

n = 3 so output should be.

1*2*3
7*8*9
4*5*6

n = 4

1*2*3*4
9*10*11*12
13*14*15*16
5*6*7*8

n = 5

1*2*3*4*5
11*12*13*14*15
21*22*23*24*25
16*17*18*19*20
6*7*8*9*10

and so on. . .

  Without using STL Stack

void showPattern(int n)
{
    int *arr = (int*)malloc(n * sizeof(int));
    int toggle = 1;
    int k;
    int top = -1;
    for (int i = 0; i < n; i++) {
        k = i*n + 1;
        if (toggle) {
            for (int j = 0; j < n; j++) {
                if (k < n || k%n)
                    cout << k << "*";
                else
                    cout << k;
                k++;
            }
            cout << endl;
            toggle = 0;
        }
        else {
            top++;
            arr[top] = i;
            toggle = 1;
        }
    }

    while (top != -1) {
        k = arr[top] * n + 1;
        for (int i = 0; i < n; i++) {
            if (k < n || k%n)
                cout << k << "*";
            else
                cout << k;
            k++;
        }
        cout << endl;
        top--;
    }

}

  Using STL Stack

void showPattern(int n)
{
    stack <int> stk;
    int itr, toggle = 1;

    for (int i = 0; i < n; i++) {

        if (toggle) {
            itr = i*n + 1;
            for (int j = 0; j < n; j++) {
                if (itr < n || itr%n) {
                    cout << itr << "*";
                }
                else {
                    cout << itr;
                }
            }
            cout << endl;
            toggle = 0;
        }
        else {
            stk.push(i);
            toggle = 1;
        }
    }

    int i;
    while (!stk.empty()) {
        itr = stk.top()*n + 1;
        stk.pop();
        for (int j = 0; j < n; j++) {
            if (itr < n || itr%n) {
                cout << itr << "*";
            }
            else {
                cout << itr;
            }
        }
        cout << endl;
    }

}

Prefer Clear Naming over using namespace std

According to the [MSDN website]:

  Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.

A collision is when 2 different functions have the same name, the same arguement types and a
similar functionallity (this is why they have the same name). Someone developing software may want
to override a function such as std::cout, std::cin or they may want to override the functionallity
of a class such as std::vector or std::stack. Namespaces allow these constructs to be overriden.

The use of the programming statement:

using namespace std;

hides the fact that cin, cout, vector and stack are coming from the namespace std where cin, cout,
vector and stack are used in the code. This can cause confusion of where the code is actually
coming from.

As the software becomes more complex and uses more libraries this becomes a bigger problem.

For a more detailed discussion of why it is a bad idea to use using namespace std;
see this stackoverflow question and this stackoverflow question.

The Use of new is Preferred Over malloc in C++
The code contains the line:

    int *arr = (int*)malloc(n * sizeof(int));

This is a C programming language construct. In C++ it is much more accepted to use

    int *a = new int[n];

Even this is out dataed because of all the container classes, and generally using pointers is
frowned upon in C++ because it is error prone.

Declare Variables When Needed
In the older versions of the C programming language one had to put the variable declarations at
the top. The current versions of C and C++ allow variables to be declared as needed.

An example of this is to move the variable toggle to be within the for loop.

Limit the Use of std::endl
It would be better to use '\n' to insert the new lines in the loops because std::endl does a file flush
each time it is called. This makes it an expensive operation.

Use Boolean Variables
In the C programming language there is no bool variable type unless one includes stdbool.h. In the
C++ programming language it would be better to implement the variable toggle as a bool variable
rather than as an int. Then you can use true and false rather than 1 and 0 respectively.


Creating a Generic Template Interface/Class and injecting into another object I would like to create a generic template class for my document processor. Assuming the template has a source file path and a "content", here's what I came up with:

Template

public interface ITemplate<T>
{
    /// <summary>
    /// The filepath of the base document for the template.
    /// </summary>
    String BaseDocumentFilePath { get; }

    /// <summary>
    /// Retrieve the underlying template source.
    /// </summary>
    /// <returns>T</returns>
    T GetContent();
}
public class WordTemplate : ITemplate<XmlDocument>
{
    private string _baseDocumentFilePath;
    private XmlDocument _content;

    public WordTemplate(XmlDocument content, string baseDocumentFilepath)
    {
        _content = content;
        _baseDocumentFilePath = baseDocumentFilepath;
    }

    public string BaseDocumentFilePath { get { return _baseDocumentFilePath;}}

    public XmlDocument GetContent()
    {
        return _content;
    }
}

Ex Doc Generator

public interface IDocumentGenerator
{
    /// <summary>
    /// Generates a document from an instance of <see cref="ITemplate"/>.
    /// </summary>
    /// <param name="template">The template for the document.</param>
    /// <param name="outputDocumentFilepath">The full file path of the result output.</param>
    /// <returns>Returns filepath for generated document.</returns>
    void Generate<T>(ITemplate<T> template, string outputDocumentFilepath);
}

public class WordDocumentGenerator : IDocumentGenerator
{
    public void Generate<T>(ITemplate<T> template, string outputDocumentFilepath)
    {
        XmlDocument xmlDocumentBody = template.GetContent() as XmlDocument;

        // generate doc
    }
}

The template would be created elsewhere and injected into the document generator. My question is, is this a good design? I am casting the generic T into XmlDocument (which is what I need for that specific implementation) and I don't think that's the right way to use generics. I've always been a little lost when it comes to generics.

The template related class and interface are looking good to me. But the generator related class and interface are flawed.

Assume one would call the Generate() method with an ITemplate<string> which would be totally legal. Sure it would work, but xmlDocumentBody would be null.

You could if the passed in template isn't a ITemplate<XmlDocument> throw some exception, but a user of this class wouldn't expect for a generic class which is called correctly to get an exception.

So you should better make the IDocumentGenerator generic and not the Generate() method.

public interface IDocumentGenerator<T>
{
    void Generate(ITemplate<T> template, string outputDocumentFilepath);
}

and now your WordDocumentGenerator class should implement IDocumentGenerator<XmlDocument> like

public class WordDocumentGenerator : IDocumentGenerator<XmlDocument>
{
    public void Generate<T>(ITemplate<XmlDocument> template, string outputDocumentFilepath)
    {
        XmlDocument xmlDocumentBody = template.GetContent();

        // generate doc
    }
}

For sure if you have different types of templates you should add a DocumentGeneratorFactory which returns the desired DocumentGenerator for the task at hand.


Converting integers to strings I drafted a method that I intend to use to convert integers to strings. Is there a way to make it faster? Are there any "simple" changes I could make to improve its performance? This cannot be done with C++11.

std::string to_string(int val) {
    static std::string str;
    static std::stringstream ss;
    ss << val;
    ss >> str;
    ss.clear();
    return str;
}

I have a big concern with this code. It uses local static variables, so this will result in race conditions if used in a multithreaded program.

I assume you did that to improve performance? Constructing the std::stringstream could potentially incur some overhead. So having that variable as a static would be understandable if you are sure no race conditions apply. But the str string certainly doesn't have to be a local static. Actually, it is not needed at all, since stringstream::str() exists.

I think you have two options here:

Make it thread-safe and cleaner at the cost of some extra object construction every call, which is probably not a big deal:

std::string to_string(int val) {
    std::stringstream ss;
    ss << val;
    return ss.str();
}

Or, if you need ultimate performance and cannot afford the chance of a memory allocation by std::stringstream, then you could use sprintf, snprintf or itoa. This is a possible implementation, also thread safe, with no statics:

std::string to_string(int val) {
    // Buffer size is arbitrary in this example. Could be smaller.
    // 128 is more than enough for any integer number.
    char buffer[128];
    std::snprintf(buffer, sizeof(buffer), "%i", val);
    return buffer;
}

I suggest going with the first one, but if you have a justifiable performance concern, then the second one should perform slightly better.


Stack implementation using arrays in Java Could someone please review my code and let me know if there is any scope for optimization for better performance.

package com.gmail.practice;

import java.util.Arrays;

public class Stacks {

private int top;
int size;
int[] stack;

public Stacks(int arraysize)
{
    size = arraysize;
    stack = new int[size];
    top = -1;
}

public void push(int value)
{
    if(top == size-1)
    {
        System.out.println("the value cant be pushed: Stack underflow");
    }
    else{
        top = top+1;
        stack[top] = value;
    }
}

public void pop()
{
    if(top > size-1)
    {
        System.out.println("stack is overflow");
    }else{
        top = top-1;
        System.out.println("current top value is " + " " +stack[top]);
        System.out.println("the popped value is" + " " + stack[top+1]);
    }

}

public void display()
{
    for(int i=0;i<size-1;i++)
        System.out.println(stack[i]);
}

public static void main(String[] args)
{
    Stacks s = new Stacks(5);
    s.push(10);
    s.push(15);
    s.push(19);
    s.push(3);
    s.pop();
    s.push(4);
    s.display();
    s.pop();
    s.pop();
    s.display();
}
}

As mentioned in @rolfl's to your later question, there is limited value in this class as elements cannot be easily retrieved.

Putting aside the obvious improvements to the seemingly toy implementations, you have a mistake in describing whether the stack has 'overflow' or 'underflow': the meanings should be inversed. When you try to push(int) to a full stack, that is known as an overflow, and you need to check whether the stack is empty when pop()-ping instead of checking for 'overflows'.

To better indicate error conditions such as those mentioned above, you can make use of throwing Exceptions instead of a simple System.out.println(). In fact, I will suggest using System.err.println() at the very least to differentiate between normal and error 'outputs'.

A Java 8 way of printing the contents of the stack is to use Arrays.stream(int[]):

public void display() {
    Arrays.stream(stack).forEach(System.out::println);
}

This uses System.out.println(int) as a method reference to print each int value on the console.

Simply running through some test operations in the main() method is also barely enough, you should consider proper unit testing techniques such as using a testing framework that can arrange-act-assert for you.

Last but not least, you should also take a look at the standard JDK classes' stack implementations for greater inspiration, such as the Deque interface.


Find and extract delimited packet in buffer I'm writing in VB.NET, using VS Community 2013.  I don't know much about this language. I have managed to make my code work, but I suspect it is possible to improve the code a lot.

This code extracts packets from some data I've received using TcpClient.

Private inbuf(10000) As Byte
Private inoff As Integer

Private Sub ProcessBuffer

    ' Buffer may contain multiple packets, delimited by a newline
    Do While 1
        Dim newline As Byte = 10
        Dim newline_index = System.Array.IndexOf(Of Byte)([inbuf], newline) ' I think this is zero-based index
        Dim Data As String = Nothing  ' if I leave this initializer out then Data seems to retain its value for next loop iteration

        If (newline_index >= 0 AndAlso newline_index < inoff) Then   ' packet found
            Data = Encoding.ASCII.GetString(inbuf, 0, newline_index)
            inbuf = inbuf.Skip(newline_index + 1).ToArray()
            ReDim Preserve inbuf(10000) ' the Skip shortened the array
            inoff = inoff - (newline_index + 1)
            If inoff < 0 Then inoff = 0 ' shouldn't happen but seems to happen sometimes?
        End If

        ' If we did not find a newline (or the input was empty string), it means we need to wait for more input
        If String.IsNullOrEmpty(Data) Then
            Return
        End If

        ' Process the response string
        handle_packet(Data)
    Loop
End Sub

Not a full review, but:

If String.IsNullOrEmpty(Data) Then is completely unnecessary. Because Data will be null if you didn't set it within the If portion. Just add an else to the previous if and have it return.

If (newline_index >= 0 AndAlso newline_index < inoff) Then   ' packet found
    ...
else
    Return
End If

Also your variable names could use some work. I actually don't know what inoff is supposed to stand for here.


User input happy birthday program For self-study homework I made a Java program that takes user input, creates people as objects, and says "Happy Birthday" to each person.

I am just wondering what mistakes I made here or how it could have been better.

PERSON CLASS

public class PeopleObjects{
    // For practice, make the people objects rather than just a list of names.

    private String name;
    private String birthDay;

    public void setName(String name){
        this.name = name;
    }

    public String getName(){
        return name;
    }

    public void setBirthDay(String myBirth){
        birthDay = myBirth;
    }

    public String getBirthDay(){
        return birthDay;
    }

}

MAIN

import java.util.Scanner;
import java.util.ArrayList;

public class PeopleObjectsTestDrive{
    public static void main(String [] args){

        ArrayList<PeopleObjects> listOfPeopleObjects = new ArrayList<PeopleObjects>();
        Scanner input = new Scanner(System.in);

        int userChoice = 0;

        System.out.println("Enter any number other than 1 to start program."+"\n");

            while ((userChoice = input.nextInt()) != 1){

                PeopleObjects people = new PeopleObjects();

                System.out.println("\n###What is the name of the person?###\n");
                input.nextLine();//for some reason we have to use this twice to avoid bug
                people.setName(input.nextLine());

                System.out.println("\n###What is their birthday?###\n");
                people.setBirthDay(input.nextLine());
                listOfPeopleObjects.add(people);

                System.out.println("\nDo you want to quit and see list? Enter 1 for yes and 0 for no\n");
            }

            System.out.println("\n\n\n\n");

            for(int i=0; i < listOfPeopleObjects.size(); i++){

                if(listOfPeopleObjects.get(i).getName().equals("Joe")){
                    System.out.println("Happy Birthday Joe! You are special! Your birthday is on "+listOfPeopleObjects.get(i).getBirthDay()+" \n");
                }else if(i%2 == 0){
                    System.out.println("Happy Birthday to "+listOfPeopleObjects.get(i).getName()+".\n");
                }else{
                    System.out.println("Happy Birthday to "+listOfPeopleObjects.get(i).getName()+" too!!!\n");

                }//end if else
            }//end for
    }
}

PeopleObjects is an odd name for a couple reasons:

Both words are plural, even though each instance only represents a single person.
Having "object" in the name is a little redundant. It's implied that the class is representing an  object.

I'd just call the class Person.

And you didn't give PeopleObjects a constructor. You're constructing an instance of the class ahead of time, then asking for input and calling the setters. What if you forgot to set the birthday or name? They would be left as null and potentially cause problems later. If properties are required (which both name and birthday would presumably be), have them supplied by a constructor:

public Person(String name, String birthDay) {
    this.name = name;
    this.birthDay = birthDay;
}

. . .

List<Person> people = new ArrayList<Person>();

Scanner input = new Scanner(System.in);

System.out.println("Enter any number other than 1 to start program."+"\n");

while (input.nextInt() != 1){
        // This is needed since nextInt doesn't consume the newline
        input.nextLine();

        System.out.println("\n###What is the name of the person?###\n");
        String name = input.nextLine();

        System.out.println("\n###What is their birthday?###\n");
        String birthday = input.nextLine();

        Person person = new Person(name, birthday);

        people.add(person);

        System.out.println("\nDo you want to quit and see list? Enter 1 for yes and 0 for no\n");
}

I changed a few things about that loop:

As mentioned above, the person isn't constructed until the necessary data is already obtained, and the data is passed directly to the constructor.
userChoice was never used. It was just storing the users entry, but that choice was only ever used in the condition. If you really wanted it for debugging purposes, it may be appropriate, but I didn't think that it was necessary here.
I renamed the list of people to simply people. This was replacing your PeopleObjects people = new PeopleObjects(); line. Again, calling a single person people is confusing. I also changed the type of people to List, from ArrayList. Always use the broadest type possible. In this case, you don't need any operations specific to ArrayList; you're only using List methods. To see why this is important, give this a read over. Note the example used in the question.

Your other loop at the bottom is sub-optimal as well. First, you're constantly writing people.get(i). For an ArrayList, get is very fast, but those calls are causing unneeded bloat. Create an intermediate variable person in the loop to neaten things up:

for(int i = 0; i < people.size(); i++) {
    Person person = people.get(i);
    String name = person.getName();

    if(name.equals("Joe")) {
        System.out.println("Happy Birthday Joe! You are special! Your birthday is on "
                           + person.getBirthDay() + " \n");

    } else if(i % 2 == 0) {
        System.out.println("Happy Birthday to " + name + ".\n");

    } else {
        System.out.println("Happy Birthday to " + name + " too!!!\n");
    }
}

I ended up creating a name variable as well since the name of the person was needed in multiple places.

You also had inconsistent spacing that I corrected. Some places had everything all smooshed together, and then other places (like with ==) had spacing. I added spaces around all the operators, then broke up one of the longer lines. I also added empty lines after each println call so the cases were a little more distinct.

Unless you need the i % 2 == 0 check to give a special message to every arbitrary second person, this loop could be slightly simplified by using an enhanced for-loop that doesn't rely on indices:

// Read as "for each person in people"
for(Person person : people) {
    String name = person.getName();

    if(name.equals("Joe")) {
        System.out.println("Happy Birthday Joe! You are special! Your birthday is on "
                           + person.getBirthDay() + " \n");

    } else {
        System.out.println("Happy Birthday to " + name + " too!!!\n");
    }
}


Displaying comments and replies retrieved from a database I created an example (for me) of how I must get the comments and replies from the database.

Here is the result for example:

//I am ONLY thinking a query like this
$q = "SELECT * FROM comments_table WHERE post = 'url_code' ORDER BY date DESC";

//An example result (Not used the query above)

$comment = array(
            array(
                'id'            => '7',
                'user'          => 'Jul',
                'post_id'       => '1',
                'comment_id'    => '7',
                'reply_id'      => '1',
                'date'          => 'Jul 2016',
                'message'       => 'hello7'
            ),
            array(
                'id'            => '6',
                'user'          => 'Jun',
                'post_id'       => '1',
                'comment_id'    => '6',
                'reply_id'      => '1',
                'date'          => 'Jun 2016',
                'message'       => 'hello6'
            ),
            array(
                'id'            => '5',
                'user'          => 'May',
                'post_id'       => '1',
                'comment_id'    => '5',
                'reply_id'      => '3',
                'date'          => 'May 2016',
                'message'       => 'hello5'
            ),
            array(
                'id'            => '4',
                'user'          => 'Apr',
                'post_id'       => '1',
                'comment_id'    => '4',
                'reply_id'      => '2',
                'date'          => 'Apr 2016',
                'message'       => 'hello4'
            ),
            array(
                'id'            => '3',
                'user'          => 'Mar',
                'post_id'       => '1',
                'comment_id'    => '3',
                'reply_id'      => '',
                'date'          => 'Mar 2016',
                'message'       => 'hello3'
            ),
            array(
                'id'            => '2',
                'user'          => 'Feb',
                'post_id'       => '1',
                'comment_id'    => '2',
                'reply_id'      => '',
                'date'          => 'Feb 2016',
                'message'       => 'hello2'
            ),
            array(
                'id'            => '1',
                'user'          => 'Jan',
                'post_id'       => '1',
                'comment_id'    => '1',
                'reply_id'      => '',
                'date'          => 'Jan 2016',
                'message'       => 'hello'
            )
        );

Then the code to output it in the page:

foreach ($comment as $key => $value) {
    if(empty($value['reply_id'])) {
        $comments[$value['comment_id']] = array('user' => $value['user'], 'message' => $value['message']);
    } else {
        $replies[] = array('id' => $value['id'], 'reply_id' => $value['reply_id'], 'user' => $value['user'], 'message' => $value['message']);
    }
}

$reply_count = count($replies);
for ($i=0; $i < $reply_count; $i++) {
    $reply_id = $replies[$i]['reply_id'];
    if(isset($comments[$reply_id])) {
        $cc[$reply_id]['comment'] = array('user' => $comments[$reply_id]['user'], 'message' => $comments[$reply_id]['message']);
        $cc[$reply_id][$replies[$i]['id']] = array('user' => $replies[$i]['user'], 'message' => $replies[$i]['message']);
    }
}

foreach ($cc as $kkk => $vvv) {
    $reply = '';
    foreach ($vvv as $kk => $vv) {
        if(is_int($kk)) {
            $reply .= $vv['user'] . ' reply: ' . $vv['message'] . '
';
        } else {
            $comment = $vv['user'] . ' comment: ' . $vv['message'] . '
';
        }
    }
    echo $comment . $reply . '
';
}

The results are okay, but my question is, is this code okay to use?

It seems you wish to batch the posted messages together into threads.  In other words, create a "parent - children" relationship.  This can be achieved simply by using one loop to generate the new data structure and one smaller loop (I use array_map()) to display the text.

Code: (Demo)

foreach ($comments as $data) {
    if ($data['reply_id']!='') {
        $threads[$data['reply_id']][]="{$data['user']} reply: {$data['message']}";   // append element
    } else {
        if (!isset($threads[$data['id']])) {  // prevent Notice
            $threads[$data['id']] = [];
        }
        array_unshift($threads[$data['id']], "{$data['user']} comment: {$data['message']}");  // prepend element
    }
}

// display data (with no unwanted line breaks):
echo implode("

", array_map(function($v){return implode("
",$v);}, $threads));

Output (same as your posted code provides):

Jan comment: hello
Jul reply: hello7
Jun reply: hello6

Mar comment: hello3
May reply: hello5

Feb comment: hello2
Apr reply: hello4

Some additional notes:

Using ORDER BY date DESC will place newer replies above older replies, be sure that this makes sense in terms of discussion "flow".
There appears to be data redundancy with id and comment_id.  If there is a difference between these two columns of data that doesn't present itself in your posted task, then they both can stay, otherwise you should omit one of these columns from your database.
A final consideration... when dealing with thread-based messages/posts/data, it simplifies tasks like these to assign a thread_id, then you can GROUP BY and/or ORDER BY the column in your SQL and spare much of the php handling.


Maximizing GCD of two arrays of numbers I am new to coding. Given int arrays A and B, I would like to find the maximum GCD (Greatest Common divisor) any element of A and any element of B. I am doing the following

Sorting both arrays, then
Comparing every value of array A to array B to find GCD
Using a temp number I am checking if the GCD found is bigger than previous.
Finally returning the value (i.e. temp)

The GCD(a,b) returns the Greatest Common divisor of a and b.

Note: I am not comparing elements of A array with A.

Is there any way to improve the performance of my Code?

I want to decrease the time for a large number of inputs of Arrays A and B in the for loops by applying some Conditions.

Example :

A=2,5,8,9

B=4,12,8,7,16

Now if we choose 8 from A and 8 from B we get maximum GCD i.e. 8 . And then returning The maximum GCD (i.e. 8 in my example).

   private static int GCD(int a, int b) {
    BigInteger b1 = BigInteger.valueOf(a);
    BigInteger b2 = BigInteger.valueOf(b);
    BigInteger gcd = b1.gcd(b2);
    return gcd.intValue();
    }

 public static int maximumGcd(int[] A, int[] B) {
    Arrays.sort(A);
    Arrays.sort(B);
    int temp = 0;
    for (int i = A.length-1; i >0; i--) {
        for (int j = A.length-1; j >0; j--) {
            int tempGcd = GCD(A[i],B[j]);
            if ( tempGcd > temp){
                temp = tempGcd;
            }
        }
    }
    return temp;
}

Bug: off by one

In both your loops, you fail to loop to index 0.  For example, if the length of array A is 5, then your loop:

for (int i = A.length-1; i >0; i--) {

will cover indices 4,3,2,1 and then stop.  It should be written like this in order to also loop to index 0:

for (int i = A.length-1; i >= 0; i--) {

Bug: array out of bounds error

You made a copy and paste error here:

    for (int j = A.length-1; j >0; j--) {

This should have been:

    for (int j = B.length-1; j >= 0; j--) {

In other words, you used the length of A where you should have used the length of B.  Thus, if array A is bigger than array B, you would index past the end of array B and get an out of bounds error.

Useless statements

Your code contains a few useless statements:

Arrays.sort(A);
Arrays.sort(B);
int sumMax = A[0]+B[0];

There is no need to sort the arrays since the order in which you loop through the arrays doesn't matter.  The sumMax variable is never used after it is initialized.

Better algorithm

I suspect this may be a current programming challenge so I won't write any code or go into too much detail here.

The current solution is \$O(N*M)\$ where \$N\$ and \$M\$ are the sizes of the arrays.  Instead of comparing one number with one number, if you could compare one number to a whole array, you could reduce your complexity by a lot.

How could you do this?  Suppose for one whole array, you compute and remember all the possible divisors for each number in that array.  Then if you go through the other array, you can compute the divisors for each number and see if each divisor appeared in the first array.  If it did, then you have found a possible GCD for an A/B pair.  If you can ensure that your divisor lookup is \$O(1)\$, then your total complexity should be:

\$O(N*F(N) + M*F(M))\$ where \$F(N)\$ is the amount of time it takes to factor a number (i.e. find its divisors).  The normal way to factor a number would take \$O(\sqrt N)\$ time, but there may be faster ways of doing it, especially if the number range is limited.


Cloud Function: Deleting from both Firestorage and Firestore in a kind of "batch" I've written code that deletes a "post" the user has sent with my Android app. A "post" is made of an image (stored in Firestorage) named A, of a Firestore document named B which is the user UID (this document contains a field that is the sum of all the posts' number of likes), and of another Firestore document named C which is the post that is going to be deleted.

So, if a user deleted a post:

A must be deleted
AND B must be updated (by decreasing the number of likes of the user by the number of likes of the post that is going to be deleted)
AND C must be deleted

If at least one of these changes can't complete, nothing must be deleted or updated and an error or an exception must be thrown to the Android app.

Problem: you would think it'd be very simple; I would have to simply use a batch write, put these 3 operations in it and then commit the batch. However... A is a Firestorage document, not a Firestore document, while B and C are Firestore documents. Thus, it's simply technically impossible to do that.

Question

I would want to know if the workaround I've found is correct or not, and if there is a way to correct it or improve it.

I would also want to know if the exceptions mechanism I use is correct (these exceptions are thrown if the client doesn't send values that are expected by the Cloud Functions script, or if the post could not be deleted): will my Android app be able to handle these exceptions and is it the way to do it? In other words: should I throw exceptions or return values likes -1, -2 (i.e.: error codes) for example?

The code

exports.deletePost = functions.https.onCall((data, context) => {

    if(!context.auth) {
        throw new functions.https.HttpsError('failed-precondition', 'The function must be called while authenticated.');
    }

    if(data.the_post === null || data.the_post === '') {
        throw new functions.https.HttpsError('invalid-argument', 'The function must be called with an adequat post.');
    }

    if(data.stored_image_name === null || data.stored_image_name === '') {
        throw new functions.https.HttpsError('invalid-argument', 'The function must be called with an adequat post.');
    }

    const the_post = data.the_post;
    const deleteDbEntry = admin_firestore.collection('list_of_slots').doc(the_post);
    const promise = deleteDbEntry.get().then(function(doc) {

        const stored_image_name = data.stored_image_name;
        const filePath = context.auth.uid + '/posts/' + stored_image_name;

        const batch = admin.firestore().batch();
        batch.delete(deleteDbEntry);
        if(doc.data().number_of_likes > 0) {
            const updateUserLikes = admin_firestore.collection("users").doc(context.auth.uid);
            batch.update(updateUserLikes, "likes", FieldValue.increment(-doc.data().number_of_likes));
        }
        const batch_commit = batch.commit();
        const deleteFile = storage.bucket('andr0d-XYZ-fXXXX.appspot.com').file(filePath).delete();

        return Promise.all([deleteFile, batch_commit]).then(function() {
            return 1;
        }).catch(function(error) {
            console.log(error);
            throw new functions.https.HttpsError('unknown', 'Unable to delete the post. (2)');
        });

    }).catch(function(error) {
        console.log(error);
        throw new functions.https.HttpsError('unknown', 'Unable to delete the post. (1)');
    });

    return promise;
});

I must admit that I haven't really used firebase or done any android development and thus cannot answer your questions about the correctness of the code. I did wonder if using a transaction would help satisfy the need to prevent deletion of items if other items cannot be deleted, but maybe the batch handles that... Maybe the process to delete the file (i.e. deleteFile) should be added to batch, although maybe that not possible because it is the firestorage document.

Good things

The code makes good use of const for values that don't get re-assigned.

Indentation is consistent and helps readability of the code.

Functions aren't overly long.

There aren't many indentation levels because the first function will exit early in an error state.

Suggestions

I don't see any comments. The code does seem mostly explanatory as it is but it would be helpful to have some insights about the code - at least function parameter types, return types, etc. Over time it might become difficult to remember why you may have done something a certain way.

Many variables are declared and then only used once. There is little point in doing this. Many of those variables can be eliminated - e.g. the_post, promise, stored_image_name, filePath, updateUserLikes.

The variable name deleteDbEntry could be confusing to other readers of your code. A more appropriate name might be dbEntryToDelete.

The lambda function passed to .then() of Promise.all() could be simplified to an arrow function - e.g.

return Promise.all([deleteFile, batch_commit]).then(_ => 1)


Simple SingleLinkedList Implementation in C++ Below I have a very simple LinkedList implementation. I was wondering if there are any memory leak problems with this and if I used the pointers correctly. I just started practicing with C++ and hoping to improve my use of pointers and memory leak management.

#include <iostream>

struct node{
    int data;
    node *next;

    node::node(){}

    node::node(const int & val, node* link)
            : data(val), next(link)
    {}
};

class LinkedList
{

    public:
        LinkedList();
        void addToEnd(int val);
        void printList();
        void deleteList(node *head);
        void deleteNode(node *toBeDeleted);
        node* get(int index);

    private:
        int size;
        node *head, *tail;

};

LinkedList::LinkedList(){
    head = NULL;
    tail = NULL;
}

node* LinkedList::get(int index){

    node* temp = head;
    int pointer = 0;

    while(pointer != index){
        temp = temp->next;
        pointer++;
    }

    return temp;

}

void LinkedList::addToEnd(int val){

    node *temp = new node(val, NULL);
    // temp->data = val;
    // temp->next = NULL;

    if(head==NULL){
        head = temp;
        tail = temp;
        temp = NULL;
    }
    else{
        tail->next = temp;
        tail = temp;
    }
}

void LinkedList::printList(){

    node *temp = head;
    //temp = head;
    while(temp){
        cout<<temp->data<<"\t";
        temp = temp->next;
    }
}

//Supposed to delete the nodes coming after the input node
void LinkedList::deleteList(node *start){
    if(start){
        deleteList(start->next);
        delete start;
    }
}

//deletes a single node
void LinkedList::deleteNode(node* toBeDeleted){

    node* temp = head;

    if(head == toBeDeleted){
        head = head->next;
        delete temp;
    }

    while(temp->next != toBeDeleted){
        temp = temp->next;
    }

    if(temp->next){
        temp->next = toBeDeleted->next;
        delete toBeDeleted;
    }

}

int main(){

    LinkedList list;

    for(int i=0;i<4;i++){
        list.addToEnd(i+1);
    }

    list.deleteList(list.get(2));
    //list.deleteNode(list.get(2));

    list.printList();

    return 0;
}

Currently, the main function doesn't run after deleteList. I am not necessarily asking about how to fix that part, I just wanted to learn tips about better pointer uses and memory leak possibilities.

Edit:

I have commented the use of deleteList and deleteNode and below I have added a destructor showing how I was planning to use deleteList.

LinkedList::~LinkedList(){
    deleteList(head);
}

You asked about pointer usage, so a topic which may interest you is:

Indirect Pointers

Indirect pointers fall into the "easy to use, difficult to think up" category, but are useful in many cases, so are worth understanding, and apply to a linked list.

In your implementation of deleting a node there are two cases, one where the head should be deleted, and one where somewhere else should be, and in both cases you do roughly the same thing. We can get rid of that duplicated code.

Instead of

node* temp = head;

we can take the pointer to head, like

node** indirect = &head;

and as we go along, we can move indirect to the pointer to this node's next pointer

indirect = &((*indirect)->next);

Put together, this results in a method which looks something like this:

void SingleLinkedList::deleteNode(node* toBeDeleted){
    node* prev = nullptr;
    node** indirect = &head;

    while(*indirect != toBeDeleted){
        prev = *indirect;
        indirect = &((*indirect)->next);
    }

    *indirect = toBeDeleted->next;
    delete toBeDeleted;

    if(!(*indirect)) tail = prev;
}

Notice that the special case can be totally removed because, in the first assignment to indirect, indirect contains a pointer to somewhere in the stack (where head is), and in future assignments it has a value in the heap (where the current node is)


php highlight search keywords [working solution]! I just spent the whole night writing this little script for my search engine. Now i want to improve it.

Any ideas?

<?php
echo "KEYWORDS : ".  $searchstring  = "the and";

$sentence = "Skype Home is unavailable at the moment. Check back later to see your news and alerts.  It's easy to start a conversation on Skype:  Choose a contact and start talking Call a phone or mobile from the dial pad, or send an SMS.";

echo "

ORIGINAL STRING

$sentence";

$kt=split(' ',$searchstring); //Breaking the string to array of words

$num=1;

while(list($key,$val)=each($kt)){
    // do not load sentence from session on first run!

    if($num<=1){
    } else {
        $sentence  = $_SESSION['NewSentence'];
    }
    if($val<>" " and strlen($val) > 0){

        // make keyword bold and send entire sentence to session

        $_SESSION['NewSentence']    = eregi_replace ("$val", "$val", $sentence);
        // output sentence with added keyword

        echo "

$num bolding:  $val

  ".$_SESSION['NewSentence'];

        $num++;
    }
}

// make all bold keywords to red`

echo '

FINAL STRING

';
echo  $_SESSION['NewSentence'] = eregi_replace ("", "", $_SESSION['NewSentence']);

//destroy  session
unset($_SESSION['NewSentence']);
session_unregister($_SESSION['NewSentence']);?>

Use of deprecated functions

Both split() and eregi_replace() are deprecated since PHP 5.3. Don't use them.

Use preg_replace() instead of eregi_replace() and explode() instead of split() Other alternatives are str_replace() and preg_split().

Unexpected results

When the search string is lowercase, all replaced words will be lowercase too. The case of the original word is not preserved.

Unused variables

The $key variable is assigned but never used. In fact, the entire while loop is odd. Use a foreach loop instead, like so:

foreach($kt as $val) {
    ...

Missing function

The code should be in a function. The function would require two parameters, the string and an array of words to highlight, and return the result string. The function should not echo anything (but I presume the echo statements are debugging things).

Old HTML

Although valid, the use of <b> tags is outdated. It describes formatting in a document layout, without specifying the need for the format. You should use html for layout and css for styling. Then you can replace the <b> with a <span class="highlight> (or any other sensible class name).


Solving Code Jam's Dice Straight I'm trying to solve the Code Jam Dice Straight problem. My Python solution seems to produce the correct output for the first few test cases but then it becomes too slow. How can I possibly optimize it?

from operator import itemgetter
from itertools import groupby

def get_straight(chunk: list, idx: int, straight: int, dice: set) -> int:
    """Get max straight starting at index idx."""
    if idx == len(chunk) - 1:
        return straight

    item = chunk[idx]

    return max((get_straight(chunk, idx+1, straight+1, dice-{x})
                for x in item[1] if x in dice), default=straight)

def main():
    T = int(input())

    for i in range(T):
        ndice = int(input())
        integers = []

        for j in range(ndice):
            integers.extend((int(x), j) for x in input().split())

        integers.sort(key=itemgetter(0))

        grouped = [(k, [x[1] for x in g])
                   for k, g in groupby(integers, itemgetter(0))]
        chunks = []

        # find runs of consecutive numbers
        for k, g in groupby(enumerate(grouped), lambda x: x[0]-x[1][0]):
            g = list(g)
            if len(g) > 1:
                chunks.append(list(map(itemgetter(1), g)))

        chunks.sort(key=len, reverse=True)

        max_straight = 1

        for chunk in chunks:
            if len(chunk) <= max_straight:
                break

            for k in range(len(chunk)-1):
                if len(chunk) - k < max_straight:
                    break
                dice = set(range(ndice))
                straight = get_straight(chunk, k, 0, dice)
                if straight > max_straight:
                    max_straight = straight

        print("Case #{}: {}".format(i+1, max_straight))

main()

Your code looks good, seems to work and is somehow documented but it can still be improved in various ways (not only performances which will be only tackled at the end if at all).

Code organisation

Your call to main could be moved behind an if __name__ == "__main__": guard in case you ever want to reuse get_straight(). It may not be relevant in your case but it is a good habit to take.

Then, it may be a good idea to split your code into smaller functions, easier to understand and to maintain.

By doing so, you'd also separate the concerns my having the code computing the results you need not mixed up with the logic handling the input/output.

Finally, and maybe even more important, that will make your code testable. Indeed, before performing any changes in the algorithm, it is highly recommended to write unit-tests to prevent you from breaking everything (tests are usually not fail-proof but they help a lot).

from operator import itemgetter
from itertools import groupby

def get_straight(chunk: list, idx: int, straight: int, dice: set) -> int:
    """Get max straight starting at index idx."""
    if idx == len(chunk) - 1:
        return straight

    item = chunk[idx]

    return max((get_straight(chunk, idx+1, straight+1, dice-{x})
                for x in item[1] if x in dice), default=straight)

def longest_straight_length(dices):
    """Return the length of the longest possible straight using `dices`."""
    integers = []
    for j, dice in enumerate(dices):
        integers.extend((val, j) for val in dice)
    integers.sort(key=itemgetter(0))

    grouped = [(k, [x[1] for x in g])
               for k, g in groupby(integers, itemgetter(0))]
    chunks = []

    # find runs of consecutive numbers
    for k, g in groupby(enumerate(grouped), lambda x: x[0]-x[1][0]):
        g = list(g)
        if len(g) > 1:
            chunks.append(list(map(itemgetter(1), g)))

    chunks.sort(key=len, reverse=True)

    max_straight = 1

    for chunk in chunks:
        if len(chunk) <= max_straight:
            break

        for k in range(len(chunk)-1):
            if len(chunk) - k < max_straight:
                break
            dice = set(range(len(dices)))
            straight = get_straight(chunk, k, 0, dice)
            if straight > max_straight:
                max_straight = straight
    return max_straight

def interactive():
    for i in range(int(input())):
        ndice = int(input())
        dices = [[int(x) for x in input().split()] for _ in range(ndice)]
        print("Case #{}: {}".format(i+1, longest_straight_length(dices)))

def unit_tests():
    test_cases = [
        ([[4, 8, 15, 16, 23, 42],
          [8, 6, 7, 5, 30, 9],
          [1, 2, 3, 4, 55, 6],
          [2, 10, 18, 36, 54, 86]],
         4),
        ([[1, 2, 3, 4, 5, 6],
          [60, 50, 40, 30, 20, 10]],
         1),
        ([[1, 2, 3, 4, 5, 6],
          [1, 2, 3, 4, 5, 6],
          [1, 4, 2, 6, 5, 3]],
         3)
    ]
    for dices, expected_result in test_cases:
        actual_result = longest_straight_length(dices)
        if actual_result != expected_result:
            print(expected_result, actual_result, dices)

if __name__ == "__main__":
    if True:
        unit_tests()
    else:
        interactive()

Rewriting the logic

The beginning of the logic creating chunks could be rewritten using a dictionnary rather than performing smart groupby and tricky index accesses. (At the end, I re-build the data structure you are using to be able to keep the code you've written). We'd get something like:

def longest_straight_length(dices):
    """Return the length of the longest possible straight using `dices`."""
    # Mapping value to list of dice indices
    number_on_dices = dict()
    for i, dice in enumerate(dices):
        for val in dice:
            number_on_dices.setdefault(val, []).append(i)

    # Consecutive chunks
    chunks = []
    for k, g in groupby(enumerate(sorted(number_on_dices.keys())), lambda x: x[0] - x[1]):
        g = list(g)
        if len(g) > 1:
            chunk = [y for (_, y) in g]
            chunks.append(chunk)
    chunks.sort(key=len, reverse=True)

    # Re-add values to be reuse existing logic for the time being
    chunks = [[(val, number_on_dices[val]) for val in chunk] for chunk in chunks]

    max_straight = 1

    for chunk in chunks:
        if len(chunk) <= max_straight:
            break

        for k in range(len(chunk)-1):
            if len(chunk) - k < max_straight:
                break
            dice = set(range(len(dices)))
            straight = get_straight(chunk, k, 0, dice)
            if straight > max_straight:
                max_straight = straight
    return max_straight

Then, it appears clearer that the first element in pairs from chunks are never used.
We can simply have:

chunks = [[number_on_dices[val] for val in chunk] for chunk in chunks]

and in get_straight:

return max((get_straight(chunk, idx+1, straight+1, dice-{x})
            for x in item if x in dice), default=straight)

Ultimately, we can simplify the code building chunks:

# Consecutive chunks
chunks = []
for k, g in groupby(enumerate(sorted(number_on_dices.keys())), lambda x: x[0] - x[1]):
    g = list(g)
    if len(g) > 1:
        chunk = [number_on_dices[y] for (_, y) in g]
        chunks.append(chunk)
chunks.sort(key=len, reverse=True)

However, the expensive part is still here, in the recursive calls to get_straight. (I haven't found a trick for this yet but current version of the code is available here: http://termbin.com/yg5l ).

Bug ?

As I was trying to come up with bigger and bigger examples with more recursive calls, I've added the following test cases:

    ([[1, 2, 3, 4, 5, 6],
      [7, 2, 3, 4, 5, 6],
      [7, 8, 3, 4, 5, 6],
      [7, 8, 9, 4, 5, 6],
      [7, 8, 9,10, 5, 6],
      [7, 8, 9,10,11, 6],
      [7, 8, 9,10,11,12]],
     7),
    ([[1, 1, 1, 1, 1, 1],
      [2, 2, 2, 2, 2, 2],
      [3, 3, 3, 3, 3, 3],
      [4, 4, 4, 4, 4, 4],
      [5, 5, 5, 5, 5, 5],
      [6, 6, 6, 6, 6, 6],
      [7, 7, 7, 7, 7, 7]],
     7),

In both cases, we could generate the sequence [1, 2, 3, 4, 5, 6, 7] of length 7. However, the code returned 6. If you want to go further from that part, the code with additional tests and performance indicators is available here: http://termbin.com/c690 .


Synchronizing billing fields with the corresponding shipping fields How can I refactor this redundant code?

$("#txtShippingAddressFullName").change(function () {
    $("#txtBillingAddressFullName").val($(this).val());
})

$("#txtShippingAddress").change(function () {
    $("#txtBillingAddress").val($(this).val());
})

$("#txtShippingPostalCode").change(function () {
    $("#txtBillingPostalCode").val($(this).val());
})

$("#txtShippingPhoneNumber").change(function () {
    $("#txtBillingPhoneNumber").val($(this).val());
})

$("#ddlShippingCity").change(function () {
    $("#ddlBillingCity").val($(this).val());
})

Since I don't know your markup, I don't know for sure if this will work, but it might

$("input[id*=Shipping]").change(function (event) {
  var billingId = this.id.replace('Shipping', 'Billing');
  $("#" + billingId).val($(this).val());
});

Basically, get every input with "Shipping" in its id, and when such an input changes, use its id to determine the corresponding billing-input.

Edit: While this solution is a neat little demonstration of the attribute-value-contains selector, it is - as the other answers have pointed out - better to use a class or data-attribute


Counting and scoring two- and three-word phrases I am writing a function to take a list of words and score 2 and 3 word phrases based on occurrence while removing any phrase occurrences with a score of less than 3.

As someone kinda new to python, i'm wondering if there is a better way to write this to do the same thing (the code works)

The resulting dictionary looks something like this...

{ ( "lorem ipsum", 4 ), ("quick brown fox", 3), (...) }

## -words- is a list of words
## -phrases- is the resulting dictionary with the phrase as a key and score as a value

words = ("lorem","ipsum","lorem","ipsum","quick","brown","fox")
phrases = dict()

for i, w in enumerate(words):

    w = re.sub('[^0-9a-zA-Z]+', '', w).lower()

    try:
        w1 = " " + re.sub('[^0-9a-zA-Z]+', '', words[ i + 1 ]).lower()
    except:
        w1 = ""

    try:
        w2 = " " + re.sub('[^0-9a-zA-Z]+', '', words[ i + 2 ]).lower()
    except:
        w2 = ""

    if w!="" and not w.isdigit() and len(w)>1:

        p = w + w1
        p2 = w + w1 + w2

        try:
            phrases[p] = phrases[p] + 1
        except:
            phrases[p] = 1

        try:
            phrases[p2] = phrases[p2] + 1
        except:
            phrases[p2] = 1

phrases = { k:v for k, v in phrases.items() if v > 2 }

rc = sorted(phrases.items(), key=lambda x: x[1], reverse = True)

So I redid your main loop to make it more pythonic.  The completed inner loop is below.  But first let us discuss some of the changes.

If space allows, build intermediate data structure to ease later processing

So this bit of code recasts the word list into a single list that is more readily processed in the main loop.

words_x = [re.sub('[^0-9a-zA-Z]+', '', w) for w in words] + ['', '']

It does:

Add some padding at the end for end of list processing.  I wasn't sure that this was actually accurate to the described algorithm, but it does emulate the existing behavior.
Perform the regex substitution only once.

If space does not allow building another data structure, the itertools module has various tools that may allow efficient iterating over a data structures, and is often worth investigating.

zip() allows processing more than one list at a time

So I restructured the loop to look like:

for w, w1, w2 in zip(words_x, words_x[1:], words_x[2:]):

This iterates through without the need for a list index and gets three adjacent words each iteration.

zip() works by iterating through each of its arguments, and presenting the first element of each list the first time it is called, and then the second element of each list, and then...  And by calling it with the same list but starting at the 1st, 2nd and 3rd elements respectively, we can get three adjacent elements each iteration.

Use collections.Counter

So python has a nifty data structure that acts a lot like a dictionary but is used for counting.  It is oddly enough called Counter.

Counter allows replacing:

try:
    phrases[p] = phrases[p] + 1
except:
    phrases[p] = 1

with:

phrases[w] += 1

Code:

from collections import Counter

words = ("lorem", "ipsum", "lorem", "ipsum", "quick", "brown", "fox")
words_x = [re.sub('[^0-9a-zA-Z]+', '', w) for w in words] + ['', '']

phrases = Counter()
for w, w1, w2 in zip(words_x, words_x[1:], words_x[2:]):

    if len(w) > 1 and not w.isdigit():
        if w1:
            w += ' ' + w1
        phrases[w] += 1
        if w2:
            w += ' ' + w2
        phrases[w] += 1

Bugs?:

Code walks off the end of the list:

The current code walks off the end of the list.  The effect of this can be seen by the fact that the single word 'fox' ends up in the phrases list.  The current code looks for any phrase that exists more than once, so this word is eventually dropped, but...

At the end of the list the last two word phrase double counted as a three word phrase

In the current code, brown fox is counted twice.

The code below fixes both terminal condition problems.

Revised Code:

from collections import Counter

words = ("lorem", "ipsum", "lorem", "ipsum", "quick", "brown", "fox")
REMOVE_EXTRA = re.compile(r'[^0-9a-zA-Z]+')
words_x = [REMOVE_EXTRA.sub('', w) for w in words] + ['']

phrases = Counter()
for w, w1, w2 in zip(words_x, words_x[1:], words_x[2:]):

    if len(w) > 1 and not w.isdigit():
        if w1:
            w += ' ' + w1
        phrases[w] += 1
        if w2:
            # only count as three word phrase if w2 is not blank
            phrases[w + ' ' + w2] += 1


Code Improvement/Insight: Should I have approached these functions differently? Animations using Javascript This code works as it is intended. What I seek is a feedback on the quality of my code. Where should I have written it differently or how should have I approached instead of the way that I have?

These functions will be moved to a separate file to be used on other pages as well. My goal is to have this code reusable as well.

I tried using onMouseOver={ani.bind(this)}

I've also tried let element = ani() but received undefined

<img className="gPic" src={img['roofRepair1'].src} alt={img['roofRepair1'].alt} id="special" onMouseOver={ani} onMouseLeave={revani}/>

//Function anime: Anime.js

function ani(p){
    let element = p.target.id
    anime({
        targets: '#'+ element,
        scale: 2,
    })
    return element;
}

function revani(p){
    let element = p.target.id
    anime({
        targets:'#'+ element,
        scale: 1
    })
}

You can reduce this code down to one function by using default arguments.

function ani(p, reverse=false){
    let element = p.target.id;
    let scale = 2;
    if (reverse)
        scale = 1;
    anime({
        targets: '#'+ element,
        scale: scale,
    })
    return element;
}

Also, don't forget your semicolons ;-P


Group By And Two Counts I'd like to know if there's any better way to write this query:

(from a in Context.Pages
join b in Context.Visitors on a.PageID equals b.PageFK
join c in Context.Visits on b.VisitorID equals c.VisitorFK
join d in Context.Signups on c.VisitID equals d.VisitFK
where
d.DateOptinUtc.HasValue && a.UserFK == USER_ID
group d by new { a.PageID, a.DateCreated, a.BaseName, a.Name }
into g
select
new ListReport
 {
     DateCreated = g.Key.DateCreated,
     BaseName = g.Key.BaseName,
     Id = g.Key.PageID,
     Name = g.Key.Name,
     Optins = g.Count(),
     Views =
         (from h in Context.Visits
          where
              h.Visitors.PageFK == g.Key.PageID
          select h).Count()
 }).AsQueryable();

Are there any performance issues writing subqueries like these? I don't how I can get that value from the groupby.

Aside from the performance issues you should make the query more readable by using relevant variable names instead of a, b, c, d, g, and h.  In addition, the ambiguous ListReport class name should be changed to something more specific like PageUsage since it contains information about a page and how many times it has been viewed/used for signup.

(from page in Context.Pages
join visitor in Context.Visitors on page.PageID equals visitor.PageFK
join visit in Context.Visits on visitor.VisitorID equals visit.VisitorFK
join signup in Context.Signups on visit.VisitID equals signup.VisitFK
where
signup.DateOptinUtc.HasValue && page.UserFK == USER_ID
group signup by new { page.PageID, page.DateCreated, page.BaseName, page.Name }
into signupsByPage
select
new PageUsage
 {
     DateCreated = signupsByPage.Key.DateCreated,
     BaseName = signupsByPage.Key.BaseName,
     Id = signupsByPage.Key.PageID,
     Name = signupsByPage.Key.Name,
     Optins = signupsByPage.Count(),
     Views =
         (from subVisit in Context.Visits
          where
              subVisit.Visitors.PageFK == signupsByPage.Key.PageID
          select subVisit).Count()
 }).AsQueryable();


Python multi-threaded kubernetes watcher My program (which eventually will be containerised) should be able to do the following:

continuously listening for deployment and daemonsets in kubernetes (2 threads)
collect info needed (like annotations)
use the info collected in order to create objects in a remote database

I could use some peer review for the thread part, which looks like this currently:

#first thread to watch for deployments
def watch_deployments():
    v1 = client.AppsV1Api()
    w = watch.Watch()
    last_seen_version = v1.list_deployment_for_all_namespaces().metadata.resource_version
    while True:
        for item in w.stream(v1.list_deployment_for_all_namespaces, pretty="pretty", resource_version=last_seen_version):
                _ = {item["object"].metadata.name:[item["object"].kind, item["object"].metadata.namespace, item["object"].metadata.resource_version, item["object"].metadata.annotations]}
                depl_lst.put(_)

def watch_daemonsets():
    v1 = client.AppsV1Api()
    w = watch.Watch()
    last_seen_version = v1.list_daemon_set_for_all_namespaces().metadata.resource_version
    while True:
        for item in w.stream(v1.list_daemon_set_for_all_namespaces, pretty="pretty", resource_version=last_seen_version):
            _ = {item["object"].metadata.name:[item["object"].kind, item["object"].metadata.namespace, item["object"].metadata.resource_version, item["object"].metadata.annotations]}
            depl_lst.put(_)

if __name__ == '__main__':
    current_obj = {}
    depl_lst = Queue()
    thread.start_new_thread(watch_deployments, ())
    thread.start_new_thread(watch_daemonsets, ())

    while True:
        for i in range(depl_lst.qsize()):
            current_obj = depl_lst.get()

            #add object is the function in order to create the item in the remote database, not listed here
            add_object()

            depl_lst.task_done()

Its the same thing with what is being done here but in this case, the asyncio was being used:
https://medium.com/@sebgoa/kubernets-async-watches-b8fa8a7ebfd4

Restructuring and consolidation

Dealing with API client instanceInstead of generating a new client.AppsV1Api instance in each separate thread - create the instance in the main thread and pass shared API client to your threads constructors:

if __name__ == '__main__':
    ...
    v1 = client.AppsV1Api()
    thread.start_new_thread(watch_deployments, (v1,))
    thread.start_new_thread(watch_daemonsets, (v1))
    ...

Both target functions watch_deployments and watch_daemonsets perform essentially the same set of actions and differ only in specific v1.list_... routine.To eliminate duplication the common behavior is extracted into a separate function say _gather_watched_metadata that will accept a particular v1.list_... function object as callable:

def _gather_watched_metadata(list_func):
    w = watch.Watch()
    last_seen_version = list_func().metadata.resource_version
    while True:
        for item in w.stream(list_func, pretty="pretty", resource_version=last_seen_version):
            metadata = item["object"].metadata
            _ = {metadata.name: [item["object"].kind, metadata.namespace,
                                 metadata.resource_version,
                                 metadata.annotations]}
            depl_lst.put(_)

As can be seen in the above function, to avoid repetitive indexing of nested attributes like item["object"].metadata it is worth to extract it into a variable at once.

Now, both target functions become just as below:

def watch_deployments(v1):
    _gather_watched_metadata(v1.list_deployment_for_all_namespaces)

def watch_daemonsets(v1):
    _gather_watched_metadata(v1.list_daemon_set_for_all_namespaces)

That can be shortened even further: both watch_... functions can be eliminated and you may run just with _gather_watched_metadata function and specific API routines passed as an argument :

...
v1 = client.AppsV1Api()
thread.start_new_thread(_gather_watched_metadata, (v1.list_deployment_for_all_namespaces,))
thread.start_new_thread(_gather_watched_metadata, (v1.list_daemon_set_for_all_namespaces,))

But to apply the last "shortening" - is up to you ...

Consuming depl_lst queueInitiating for loop with range in this context:

while True:
    for i in range(depl_lst.qsize()):
        ...

is redundant as it's enough to check if queue is not empty:

while True:
    if not depl_lst.empty():
        current_obj = depl_lst.get()


Python - A Regex Text File Searcher by Content Project I would like some advice on efficiency and ways to further remove redundant code.
Regex Text File Search:

enter a valid directory path, and a string to search for

makes a list containing all the matched paths, and a dictionary with the matches of the matched paths

get a list of all the text files and go through them

for each text file, search for a match, line by line

if a match is found, then the path is appended to the matched_paths list

update the matched_dict to include the lines and the start index and end index for each match in the matched path

format the output into an appropriate form and display the results of the find

from pathlib import Path
from pyinputplus import inputCustom, inputStr
import re

# 2)
def check_path(path):
    """
    returns the directory if the path is a directory, and it exists otherwise asks to input again
    """
    directory = Path(path)
    if directory.is_dir():
        return directory
    else:
        raise Exception('Path is not a valid directory')

def format_output(matched_paths, matched_dict):
    """
    - outputs the directories and matches for the string to be searched, only outputs the matched text files
    Formats the output into this template:

        file1.txt
        >>> --------------------------- <<<
              ------ Line 280 ------
             (start_index, end_index) # match 1
             (start_index, end_index) # match 2 if it exists

        <<< --------------------------- >>>

        file2.txt
        ...

    """
    for path in matched_paths:
            print()
            print(path.name)
            print('>>> --------------------------- <<<')
            for line, matches in matched_dict[path].items():
                print(f'      ------ Line {line} ------')
                for match in matched_dict[path][line]:
                    print('             ', match)
                print()
            print('<<< --------------------------- >>>')

def search_for_string(path, string):
    """
    1) opens the string

    2) makes a check on if a match exists in the file

    3) if it does, goes line by line

    4) appending all the matches (start_index, end_index) to the line number in the dict of path

    returns True if match was found, so it will be appended to the matched_paths list
        matched_paths = [path1, path2, path3]

        matched_dict = {path1: {line1: [(s1, e1), (s2, e2)] }, path2: ... }
    """
    global matched_dict
    with open(path, 'r') as text_file:
        if re.search(string, path.read_text()):
            matched_dict[path] = dict()
            for i, line in enumerate(text_file.readlines()): # i refers to line number
                matches = list(re.finditer(string, line))
                if matches:
                    matched_dict[path][i] = []

                    for m in matches:
                        matched_dict[path][i].append(m.span())
            return True
        return False

# 1)
path = inputCustom(check_path, prompt='Enter Path: ')
string = inputStr(prompt='Enter string: ')

# 2)
matched_paths = []
matched_dict = dict()

# 3)
for text_file in path.glob('*.txt'):

    # 4) and 6)
    if search_for_string(text_file, string):

        # 5)
        matched_paths.append(text_file)
# 7)
format_output(matched_paths, matched_dict)

Documentation
"""
returns the directory if the path is a directory, and it exists otherwise asks to input again
"""

OK; but that's not what this function does at all. The "asking to input again" is done elsewhere.
Type hints
Guessing for this signature; type hints would remove the guesswork:
def format_output(matched_paths, matched_dict):

could become
def format_output(
    matched_paths: Iterable[str],
    matched_dict: Dict[
        str,
        Dict[str, str]
    ],
):

If this is true, the second type is complex enough that it could benefit from external declaration, i.e.
MatchesDict = Dict[
    str,
    Dict[str, str]
]

Dictionary iteration
for path in matched_paths:
    for line, matches in matched_dict[path].items():
        for match in matched_dict[path][line]:

should be
for path in matched_paths:
    for line, matches in matched_dict[path].items():
        for match in matches:

In other words, items gets you a key and a value; when you have the value, use it.
Logic inversion
I find that this:
    if re.search(string, path.read_text()):
        matched_dict[path] = dict()
        for i, line in enumerate(text_file.readlines()): # i refers to line number
            matches = list(re.finditer(string, line))
            if matches:
                matched_dict[path][i] = []

                for m in matches:
                    matched_dict[path][i].append(m.span())
        return True
    return False

is more legible as
    if not re.search(string, path.read_text()):
        return False

    matched_dict[path] = dict()
    for i, line in enumerate(text_file.readlines()): # i refers to line number
        matches = list(re.finditer(string, line))
        if matches:
            matched_dict[path][i] = []
             for m in matches:
                matched_dict[path][i].append(m.span())
    return True


Parsing command line arguments in Perl I use this idiom to parse command line arguments in Perl:

my @args;
my $encode = 1;

while (@ARGV) {
    for (shift(@ARGV)) {
        ($_ eq '-h' || $_ eq '--help') && do { &usage(); };
        # ... more options here
        ($_ eq '--') && do { push(@args, @ARGV); undef @ARGV; last; };
        ($_ =~ m/^-.+/) && do { print "Unknown option: $_\n"; &usage(); };
        push(@args, $_);
    }
}

This let's me do things like:

Supports command line arguments in arbitrary order
Supports using -- to use any following arguments "as is", without parsing
Stop parsing and print usage message on -h, or --help
Raise error if a -something flag is detected that is not matched by any of the parsing rules

Here's a short example script using this idiom:

#!/usr/bin/env perl

use strict;
use warnings;

&usage() unless @ARGV;

my @args;
my $encode = 1;

while (@ARGV) {
    for (shift(@ARGV)) {
        ($_ eq '-h' || $_ eq '--help') && do { &usage(); };
        ($_ eq '-D' || $_ eq '--decode') && do { $encode = ! $encode; last; };
        ($_ eq '--') && do { push(@args, @ARGV); undef @ARGV; last; };
        ($_ =~ m/^-.+/) && do { print "Unknown option: $_\n"; &usage(); };
        push(@args, $_);
    }
}

sub usage {
    $0 =~ m|[^/]+$|;
    print "Usage: $& [-h|--help] [-D|--decode]\n";
    print "\n";
    print "Encode (= default) or decode Base64\n";
    exit 1;
}

use MIME::Base64;
if ($encode) {
    print map(&MIME::Base64::encode_base64($_), @args);
} else {
    print map(&MIME::Base64::decode_base64($_), @args);
}

Is this a good technique for parsing command line arguments in Perl?
Is there a better way?

I'm also interested in improvement ideas about the short script itself too.

There are several modules on CPAN that are made for this task. They all reside in the Getopt namespace. Getopt::Long even comes with the standard distribution. So coming to your question: Yes, there are better ways.

If you are interested in something more fancy, you could have a look at Getopt::Declare where you can write the usage page and fill your variables in the same place. It has a downside, though since it introduces another DSL.

What I definitely would recommend is that you take a look at <<HERE docs for printing longer texts. Some people dislike them,because of their strange syntactic properties, but they are the usual way to express multiline strings in perl.