You need to diversify your strings Challenge:

  Write a program which prints all the permutations of a string in alphabetical order.

Specifications:

  Your program should accept a file as its first argument.
   The file contains input strings, one per line.
   Print the permutations of the string separated by commas, in alphabetical order.
   We consider that digits < upper case letters < lower case letters.
   The sorting should be performed in ascending order.

Solution:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class PermutateString {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner input = new Scanner(new File(args[0]));

        while (input.hasNextLine()) {
            System.out.println(
                permutateString(sortString(input.nextLine()))
            );
        }
        input.close();
    }

    private static void permutate(String prefix, String str, StringBuilder sb) {
        int len = str.length();

        if (len == 1) {
            sb.append(',').append(prefix).append(str);
        } else {
            for (int i = 0; i < len; i++) {
                permutate(prefix + str.charAt(i), str.substring(0, i) + str.substring(i + 1, len), sb);
            }
        }
    }

    private static String permutateString(String input) {
        StringBuilder characterBuilder = new StringBuilder();
        permutate("", input, characterBuilder);

        return characterBuilder.substring(1);
    }

    private static String sortString(String input) {
        char[] temp = input.toCharArray();
        Arrays.sort(temp);
        return String.valueOf(temp);
    }
}

Sample Input:

hat
abc
Zu6

Sample Output:

aht,ath,hat,hta,tah,tha
abc,acb,bac,bca,cab,cba
6Zu,6uZ,Z6u,Zu6,u6Z,uZ6

I first started solving this using a list of lists, but that required a lot of loops within loops and making size! lists seemed memory intensive..and confusing, This marks the first instance where I find a problem should be solved using recursion.

Generally wondering if there are any improvements I could make to solve this in a faster, cleaner, or memory efficient way.

For those interested, the source is CodeEval.com.

P.S.
If it seems like Java 8 features were suggested before, yet not used here, there's a reason for that. I appreciate and take all feedback to heart it's just that CodeEval only uses Java 7. So, please don't think I'm intentionally disregarding the excellent advice I receive and nonetheless welcome.

Not much to comment on the permutation, but generally speaking you should use try-with-resources, Files.lines() and a healthy dose of Java 8 features for your main() method:

public static void main(String[] args) throws FileNotFoundException {
    try(Stream<String> lines = Files.lines(Paths.get(args[0]))) {
        lines.map(PermutateString::sortString)
                .map(PermutateString::permutateString)
                .forEach(System.out::println);
    } catch (IOException e) {
        // do something with this, e.g. show file not found message?
    }
}

Please check the use of Paths.get() for your case too...


Python logging - wrapper I am new to writing utility functions. I wrote a python program to return a Python logger with custom settings. Please review this code and provide comments on how to improve it. I am looking for comments on

Coding Style
Coherence & Coupling
Readability

I am intending to share this code across all projects. So any comments on this to improve are highly appreciated.

import logging
import sys
import logging.handlers

def get_default_log_format():
    """ Method to return the default log format"""
    log_format = logging.Formatter("%(module)s - %(asctime)s — %(name)s — %(levelname)s - %(funcName)s:%(lineno)d — %(message)s")
    return log_format

def get_new_console_handler(log_format=None):
    """This method returns a console handler with the given log_format
        Keyword Arg: log_format accepts logging.Formatter object.
    """
    console_handler = logging.StreamHandler(sys.stdout)
    if log_format is None:
        log_format = get_default_log_format()
    console_handler.setFormatter(log_format)
    return console_handler

def get_new_file_handler(log_format=None, log_file_name="out.log"):
    file_handler = logging.FileHandler(log_file_name)
    if log_format is None:
        log_format = get_default_log_format()
    file_handler.setFormatter(log_format)
    return file_handler

def get_new_rotating_file_handler(log_file_name="out.log", max_log_bytes=2000000, max_log_backup_files=20, log_format=None):
    """ This method returns a file handler with the given file name, max_log_bytes, max_log_backup_files and log_format. This log_format accepts logging.Formatter object"""
# Here, max_log_bytes is the maximum size of file in Bytes and max_log_backup_files is the number of backup files to store.
    file_handler = logging.handlers.RotatingFileHandler(log_file_name, maxBytes=max_log_bytes, backupCount=max_log_backup_files)
    if log_format is None:
        log_format = get_default_log_format()
    file_handler.setFormatter(log_format)
    return file_handler

def get_new_console_logger(logger_name, log_format=None, log_level=logging.DEBUG):
    handler = get_new_console_handler()
    logger = logging.getLogger(logger_name)
    logger.setLevel(log_level)
    logger.addHandler(handler)
    return logger

def get_new_file_logger(logger_name, log_file, log_format=None, log_level=logging.DEBUG):
    handler = get_new_file_handler(log_format, log_file)
    if log_file is None:
        log_file = "execution.log"
    logger = logging.getLogger(logger_name)
    logger.setLevel(log_level)
    logger.addHandler(handler)
    return logger

def get_new_file_console_logger(logger_name, log_file, log_format=None, log_level=logging.DEBUG):
    if log_file is None:
        log_file = "execution.log"
    logger = get_new_file_logger(logger_name, log_file)
    console_handler = get_new_console_handler()
    logger.addHandler(console_handler)
    return logger

if __name__ == "__main__":
    # To get a console handler, run this code
    console_logger = get_new_console_logger(logger_name="logger1")
    console_logger.debug("Hello World")

    # To get a file logger, run this code
    file_logger = get_new_file_logger(logger_name="logger3", log_file="out.log")
    file_logger.critical("This is a critical message")

    # To get a console and file logger, run this code
    console_file_logger = get_new_file_console_logger(logger_name="my_logging2", log_file="out3.log")
    console_file_logger.error("Error: This is from console file logger")

    # logger.info("Begining of execution")
    # logger.info("logging Info message")
    # logger.debug("This is a debug message")
    # logger.debug(logging.DEBUG)
    # logger.info(logging.INFO)
    # logger.warning(logging.WARNING)
    # logger.error(logging.ERROR)
    # logger.critical(logging.CRITICAL)
    # logger.info("End of Execution")

Restructuring and consolidation

Logging configuration

When having a constant logging options like posted log_format or log_level=logging.DEBUG you might have been used logging.basicConfig or even dictConfig to apply specified options globally to all underlying cases.But since your intention is to allow the client create different specialized loggers with dynamic logging options, "base" config won't give much benefit. At least, it's good to remind of such a base configuration capability.

Handlers

All 3 handlers run the same common set of statements to ensure logging format:

if log_format is None:
    log_format = get_default_log_format()
console_handler.setFormatter(log_format)

That calls for consolidation and reducing duplication.Moreover, when creating some concrete logger instance a client may pass log_format as a raw format string like log_format='%(funcName)s:%(lineno)d — %(message)s', not as logging.Formatter instance. Thus, for further optimizations I'll eliminate get_default_log_format function and declare a constant:

LOG_FMT = '%(module)s - %(asctime)s — %(name)s — %(levelname)s - %(funcName)s:%(lineno)d — %(message)s'

According to the scenario where the client/caller operates only on .._logger functions to instantiate concrete loggers, that points to direct relation between specific logger and respective handler(s). And assuming that duplicated common code should be moved out from all handlers - they ceased pulling their weigh and are removed in favor of direct handler usage. See the final implementation below.

..._logger functions

"logger" creation functions perform a common set of actions:

logger = logging.getLogger(logger_name)
logger.setLevel(log_level)
logger.addHandler(handler)

that also calls for consolidation.The condition

if log_file is None:
    log_file = "execution.log"

is eliminated by simply making log_file a default argument log_file="execution.log".

To perform consolidation and reducing duplication a factory function logger_factory(logger_name, handlers_list, log_format, log_level) is introduced. It concentrates a common behavior and also allows to attach multiple handlers to the same logger.

The final optimized version:

import logging
import sys
import logging.handlers

LOG_FMT = '%(module)s - %(asctime)s — %(name)s — %(levelname)s - %(funcName)s:%(lineno)d — %(message)s'

def logger_factory(logger_name, handlers_list, log_format, log_level):
    logger = logging.getLogger(logger_name)
    logger.setLevel(log_level)

    if not isinstance(handlers_list, (list, tuple)):
        handlers_list = [handlers_list]
    for handler in handlers_list:
        handler.setFormatter(logging.Formatter(log_format))
        logger.addHandler(handler)

    return logger

def create_console_logger(logger_name, log_format=LOG_FMT, log_level=logging.DEBUG):
    return logger_factory(logger_name, handlers_list=[logging.StreamHandler(sys.stdout)],
                          log_format=log_format, log_level=log_level)

def create_file_logger(logger_name, log_file="execution.log", log_format=LOG_FMT, log_level=logging.DEBUG):
    return logger_factory(logger_name, handlers_list=[logging.FileHandler(log_file)],
                          log_format=log_format, log_level=log_level)

def create_rotating_file_logger(logger_name, log_file="out.log", max_log_bytes=2000000, max_log_backup_files=20,
                                log_format=LOG_FMT, log_level=logging.DEBUG):
    """ Creates rotating file logger with the given file name, max_log_bytes, max_log_backup_files and log_format.
         :param log_file: log file name
         :param max_log_bytes: the maximum size of file in Bytes
         :param max_log_backup_files: the number of backup files to store
         :param log_format: custom format as logging.Formatter object
         :param log_level: logging level
         :return: logging.Logger
     """
    handler = logging.handlers.RotatingFileHandler(log_file, maxBytes=max_log_bytes,
                                                   backupCount=max_log_backup_files)
    return logger_factory(logger_name, handlers_list=[handler],
                          log_format=log_format, log_level=log_level)

def create_file_console_logger(logger_name, log_file="execution.log", log_format=LOG_FMT, log_level=logging.DEBUG):
    handlers = [logging.FileHandler(log_file), logging.StreamHandler(sys.stdout)]
    return logger_factory(logger_name, handlers_list=handlers,
                          log_format=log_format, log_level=log_level)

if __name__ == "__main__":
    # To get a console handler, run this code
    console_logger = create_console_logger(logger_name="logger1")
    console_logger.debug("Hello World")

    # To get a file logger, run this code
    file_logger = create_file_logger(logger_name="logger3", log_file="out.log")
    file_logger.critical("This is a critical message")

    # To get a console and file logger, run this code
    console_file_logger = create_file_console_logger(logger_name="my_logging2", log_file="out3.log")
    console_file_logger.error("Error: This is from console file logger")


Trading System with Akka Recently I did an exercise to understand Akka. I implemented a trading system with Akka to process orders with Actors.

My code is like:

Order: Immutable class with acts as the messages in the system.

public final class Order {

    public enum OrderType {
        BUY,
        SELL
    };

    private final int orderId;

    private final OrderType orderType;

    private final String instrument;

    private final double price;

    public Order(int orderId, OrderType orderType, String instrument, double price) {
        this.orderId = orderId;
        this.orderType = orderType;
        this.instrument = instrument;
        this.price = price;
    }

    public int getOrderId() {
        return orderId;
    }

    public OrderType getOrderType() {
        return orderType;
    }

    public String getInstrument() {
        return instrument;
    }

    public double getPrice() {
        return price;
    }
}

Then I created an OrderProcessingActor to process the orders received. For now, the process is an abstract thing which only prints the order received and tells the sender order executed information.

import akka.actor.AbstractActor;
import akka.actor.Props;

public class OrderProcessingActor extends AbstractActor {

    public static Props props() {
        return Props.create(OrderProcessingActor.class, OrderProcessingActor::new);
    }

    @Override
    public Receive createReceive() {
        return receiveBuilder()
                .match(
                        Order.class,
                        order -> {
                            processOrder(order);
                        }).build();
    }

    private void processOrder(Order order) {
        System.out.println(String.format("OrderProcessingActor %d %s order received for instrument %s with price %s", order.getOrderId(), order.getOrderType(), order.getInstrument(), order.getPrice()));
        //process order here.
        getSender().tell(String.format("OrderProcessingActor %d %s order processed for instrument %s with price %s", order.getOrderId(), order.getOrderType(), order.getInstrument(), order.getPrice()), getSelf());
    }
}

I also created a TradingSystemSupervisor which is responsible for creating and managing OrderProcessingActor. The orders are received by the TradingSystemSupervisor which are then sent to the OrderProcessingActor.

import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.Props;

import java.util.HashMap;
import java.util.Map;

public class TradingSystemSupervisor extends AbstractActor {

    private static Map<String, ActorRef> actorRefMap = new HashMap<>();

    public static Props props() {
        return Props.create(TradingSystemSupervisor.class, TradingSystemSupervisor::new);
    }

    @Override
    public Receive createReceive() {
        return receiveBuilder()
                .match(
                        Order.class,
                        order -> {processOrder(order);}).build();
    }

    private void processOrder(Order order) {
        System.out.println(String.format("TradingSystemSupervisor %d, %s order received for instrument %s with price %s", order.getOrderId(), order.getOrderType(), order.getInstrument(), order.getPrice()));
        //process order here.
        if(!actorRefMap.containsKey(order.getInstrument())) {
            ActorRef actorRef = getContext().getSystem().actorOf(OrderProcessingActor.props(), String.format("%s-order-processing-actor", order.getInstrument()));
            actorRefMap.put(order.getInstrument(), actorRef);
        }
        actorRefMap.get(order.getInstrument()).tell(order, getSelf());
    }
}

I am looking for a review of the solution from Java as well as Akka's perspective. Is my solution ok considering the way I have used Akka or can I improve the solution? I am also trying to implement Akka routing to this solution.

Other than maybe adding some comments for documentation, this seems pretty sane. This:

    System.out.println(String.format("OrderProcessingActor %d %s order received for instrument %s with price %s", order.getOrderId(), order.getOrderType(), order.getInstrument(), order.getPrice()));
    //process order here.
    getSender().tell(String.format("OrderProcessingActor %d %s order processed for instrument %s with price %s", order.getOrderId(), order.getOrderType(), order.getInstrument(), order.getPrice()), getSelf());

is a classic don't-repeat-yourself situation. Make a toString() method on Order:

String toString() {
   return String.format("OrderProcessingActor %d %s order for instrument %s with price %f", orderId, orderType, instrument, price);
}

Then, the prior code can be

    System.out.println(String.format("%s has been received", order));
    //process order here.
    getSender().tell(String.format("%s has been processed", order));


Timer in pure JavaScript I was working on a timer code in pure JavaScript and would like to know any pointers to improve:

Features

Start/ Stop/ Reset on click of a button.
Set limit to clock.
Update class name for warning and error based on threshold timer.

JSFiddle

function timer() {
  var time = {
    sec: 00,
    min: 00,
    hr: 00
  };
  var finalLimit = null,
    warnLimit = null,
    errorLimit = null;
  var max = 59;
  var interval = null;

  function init(_hr, _min, _sec) {
    time["hr"] = _hr ? _hr : 0;
    time["min"] = _min ? _min : 0;
    time["sec"] = _sec ? _sec : 0;
    printAll();
  }

  function setLimit(fLimit, wLimit, eLimit) {
    finalLimit = fLimit;
    warnLimit = wLimit;
    errorLimit = eLimit;
  }

  function printAll() {
    print("sec");
    print("min");
    print("hr");
  }

  function update(str) {
    time[str] ++;
    time[str] = time[str] % 60;
    if (time[str] == 0) {
      str == "sec" ? update("min") : update("hr");
    }
    print(str);
  }

  function print(str) {
    var _time = time[str].toString().length == 1 ? "0" + time[str] : time[str];
    document.getElementById("lbl" + str).innerHTML = _time;
  }

  function validateTimer() {
    var className = "";
    var secs = time.sec + (time.min * 60) + (time.hr * 60 * 60);
    if (secs >= finalLimit) {
      stopTimer();
    } else if (secs >= errorLimit) {
      className = "error";
    } else if (secs >= warnLimit) {
      className = "warn";
    }
    var element = document.getElementsByTagName("span");
    document.getElementById("lblsec").className = className;
  }

  function startTimer() {
    init();
    if (interval) stopTimer();
    interval = setInterval(function() {
      update("sec");
      validateTimer();
    }, 1000);
  }

  function stopTimer() {
    window.clearInterval(interval);
  }

  function resetInterval() {
    stopTimer();
    time["sec"] = time["min"] = time["hr"] = 0;
    printAll();
    startTimer();
  }

  return {
    'start': startTimer,
    'stop': stopTimer,
    'reset': resetInterval,
    'init': init,
    'setLimit': setLimit
  }
};

var time = new timer();

function initTimer() {
  time.init(0, 0, 0);
}

function startTimer() {
  time.start();
  time.setLimit(10, 5, 8);
}

function endTimer() {
  time.stop();
}

function resetTimer() {
  time.reset();
}
span {
  border: 1px solid gray;
  padding: 5px;
  border-radius: 4px;
  background: #fff;
}
.timer {
  padding: 2px;
  margin: 10px;
}
.main {
  background: #efefef;
  padding: 5px;
  width: 200px;
  text-align: center;
}
.btn {
  -webkit-border-radius: 6;
  -moz-border-radius: 6;
  border-radius: 6px;
  color: #ffffff;
  font-size: 14px;
  background: #2980b9;
  text-decoration: none;
  transition: 0.4s;
}
.btn:hover {
  background: #3cb0fd;
  text-decoration: none;
  transition: 0.4s;
}
.warn {
  background: yellow;
}
.error {
  background: red;
}

      00
    : 00
    : 00

  <button class="btn" onclick="startTimer()">Start</button>
  <button class="btn" onclick="endTimer()">Stop</button>
  <button class="btn" onclick="resetTimer()">Reset</button>

It looks really good! Here are a few points;

I don't like the arguments in your setLimit and init functions, especially because some of them are optional.

I would prefer an Object argument in this case.

function setLimit(limits) {
  finalLimit = limits.final;
  warnLimit = limits.warn;
  errorLimit = limits.error;
}

this.setLimit({
  final: 10,
  warn: 5,
  error: 8
});

This makes it obvious at call time which argument is which.

Your var declarations are inconsistent here:

var finalLimit = null,
  warnLimit = null,
  errorLimit = null;
var max = 59;
var interval = null;

Pick either to comma separate them or not. Not both.

In a couple of places you use == instead of ===. Generally we stick to === for reasons.

You CSS selectors are very generic. This is generally a bad thing as they will easily collide with other styles the moment you integrate it into a real page.

I favour BEM syntax, which would make it look something like this;

      00
    : 00
    : 00

  <button class="timer__btn js-start-timer">Start</button>
  <button class="timer__btn js-end-timer">Stop</button>
  <button class="timer__btn js-reset-timer">Reset</button>

Note I've also removed your onclick handlers in favour of interaction specific classes. I prefix my classnames with js- if they are used solely for JavaScript selectors. This helps decouple the CSS and JavaScript.

In your code you then attach the handlers;

document.querySelectorAll('.js-start-timer')[0]
  .addEventListener('click', startTimer);


Generic Repository For Web Apps I was developing a web app using Entity Framework 6 and MVC 5. For the data access layer, I eased the job and wrote a generic repository as following:

public class GenericRepository<T> where T : class
{
    private EFDbContext context = new EFDbContext();

    public IEnumerable<T> GetAll()
    {
        return context.Set<T>();
    }
    public T GetByID(object id)
    {
        return context.Set<T>().Find(id);
    }
    public void Insert(T entity)
    {
        context.Set<T>().Add(entity);
        context.SaveChanges();
    }
    public void Update(T entity)
    {
        context.Entry(entity).State = System.Data.Entity.EntityState.Modified;
        context.SaveChanges();
    }
    public T Delete(T entity)
    {
        if (entity != null)
            context.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
        context.SaveChanges();
        return entity;
    }
}

In controllers, I declare it as a global variable and use it in different action methods.

There is one big problem with this approach:

Flexibility: I'm not able to do anything specific with this repository, like preserving some fields, etc. I'm forced to use some hacks to achieve things like that.

I know that many believe using a repository pattern with context is not useful. The main reason that I tried that was to make my controllers cleaner and a generic one to save time. But I guess it doesn't worse it. I think next time I'll just use the EF context without any additional pattern.

What do you suggest, using this library, using this with Unit of Work, or something else? Is there any other problem with this code?

One major drawback...

public void Insert(T entity)
{
    context.Set<T>().Add(entity);
    context.SaveChanges();
}

...is that you've just destroyed the usefulness of a unit of work. DbContext wraps a database transaction, and what's great about transactions, is that they allow you to commit a "set" of changes all at once.

But you're calling SaveChanges every time anything happens to any entity, which means if you have 1 request doing 20 calls into that repository, you're effectively committing 20 transactions.

A new unit of work instance should be created, and die with each request: SaveChanges should be called, ideally, exactly once in the lifetime of the DbContext.


Calculator using Shunting Yard algorithm I have implemented a simple calculator using the Shunting Yard algorithm. It takes infix expression as an input "3 + 5 * 2", which evaluates and returns the value 13.

public class ShuntingYardAlgorithm{
    private static  Map<String, Operator> operatorMap = initializeOperatorMap();

    private static Map<String, Operator> initializeOperatorMap()
    {
        operatorMap = new HashMap<>();
        operatorMap.put(Operator.ADD.sign, Operator.ADD);
        operatorMap.put(Operator.SUBTRACT.sign, Operator.SUBTRACT);
        operatorMap.put(Operator.MULTIPLY.sign, Operator.MULTIPLY);
        operatorMap.put(Operator.DIVIDE.sign, Operator.DIVIDE);
        return operatorMap;
    }

    public ShuntingYardAlgorithm()
    {
        // Default Constructor
    }

    private enum Operator
    {
        ADD(1, "+"),
        SUBTRACT(2, "-"),
        MULTIPLY(3, "*"),
        DIVIDE(4, "/");

        final int precedence;
        final String sign;

        Operator(int precedence, String sign)
        {
            this.precedence = precedence;
            this.sign = sign;
        }
    }

    private static boolean isHighPrecedence(String currentToken, String peekedOperator)
    {
        return operatorMap.containsKey(peekedOperator) && operatorMap.get(peekedOperator).precedence >= operatorMap.get(currentToken).precedence;
    }

    public static Integer evaluateExpression(String infix)
    {
        Stack<String> operatorStack = new Stack<>();
        Stack<Integer> outputStack = new Stack<>();

        for (String currentToken : infix.split("\\s"))
        {

            if ("(".equalsIgnoreCase(currentToken))
            {
                operatorStack.push(currentToken);
            } else if (operatorMap.containsKey(currentToken))
            {
                while (!operatorStack.isEmpty() && isHighPrecedence(currentToken, operatorStack.peek()))
                {
                    String higherPrecedenceOperator = operatorStack.pop();
                    Integer operandLeft = outputStack.pop();
                    Integer operandRight = outputStack.pop();
                    outputStack.push(evaluate(operandLeft, operandRight, higherPrecedenceOperator));

                }
                operatorStack.push(currentToken);
            } else if (currentToken.equalsIgnoreCase(")"))
            {
                while (!"(".equalsIgnoreCase(operatorStack.peek()))
                {
                    String higherPrecedenceOperator = operatorStack.pop();
                    Integer operandLeft = outputStack.pop();
                    Integer operandRight = outputStack.pop();
                    outputStack.push(evaluate(operandLeft, operandRight, higherPrecedenceOperator));
                }
                operatorStack.pop();
            } else
            {
                outputStack.push(Integer.valueOf(currentToken));
            }
        }

        while (!operatorStack.empty())
        {
            String higherPrecedenceOperator = operatorStack.pop();
            Integer operandRight = outputStack.pop();
            Integer operandLeft = outputStack.pop();
            outputStack.push(evaluate(operandLeft, operandRight, higherPrecedenceOperator));

        }
        return outputStack.pop();
    }

    private static Integer evaluate(Integer operandLeft, Integer operandRight, String operator)
    {
        BigDecimal retVal = BigDecimal.ZERO;
        BigDecimal left = BigDecimal.valueOf(operandLeft);
        BigDecimal right = BigDecimal.valueOf(operandRight);
        if (operator.equals(Operator.MULTIPLY.sign))
        {
            retVal = left.multiply(right);
        } else if (operator.equals(Operator.ADD.sign))
        {
            retVal = left.add(right);
        } else if (operator.equals(Operator.SUBTRACT.sign))
        {
            retVal = left.subtract(right);
        } else if (operator.equals(Operator.DIVIDE.sign))
        {
            retVal = left.divide(right);
        }
        return retVal.intValue();
    }
}

Minor remarks:

The java convention is to put the opening { on the same line, not on a new line. It's great that you're consistent in your style though!
If you're doing the calculations using BigDecimal anyway, why not do that all the way and store the intermediat values as BigDecimal's as well in your stack?
method name isHighPrecedence wasn't immediately clear to me which one was the higher precedence. Not sure how to fix this one easily though.

Bigger idea:

I like how you used an enum for the Operators. This gives a nice list of all the possible (known) operators and an easy place to check if things like precedence are implemented correctly.

What I would do though, is go a step further. In java, an Enum is still a full class (except that all instances are created on class loading, you can't create new instances). This means that you can also provide methods there. I would expect the precedence comparison to be put in there. Although initially this might still be a bit clunky with the bracket handling (more on that later).

Another nice candidate is parsing an operator from a string. This can easily be implemented as follows:

    public static Operator from(String oper) {
        for (Operator operator : Operator.values()) {
            if (operator.sign.equalsIgnoreCase(oper)) {
                return operator;
            }
        }
        return NIL;
    }

What most people don't know, is that you can also override methods for each of the instances. The evaluate is a perfect candidate for this one. For example:

private enum Operator
{
    ADD(1, "+") {
        @Override
        public BigDecimal evaluate(BigDecimal left, BigDecimal right) {
            return left.add(right);
        }
    },
    SUBTRACT(2, "-") {
        @Override
        public BigDecimal evaluate(BigDecimal left, BigDecimal right) {
            return left.subtract(right);
        }
    },
    ... [include all operators here]
    };

    final int precedence;
    final String sign;

    Operator(int precedence, String sign)
    {
        this.precedence = precedence;
        this.sign = sign;
    }

    public abstract BigDecimal evaluate(BigDecimal left, BigDecimal right);

That way, if you have an operator you can just call
outputStack.push(higherPrecedenceOperator.evaluate(operandLeft, operandRight));

Changing the operator stack to actually contain Operator instead of String got me in a little bit of trouble storing the opening bracket on there. Since the idea is to first parse the operand from the input string, let's also add an Operand for the closing bracket, and a default option for a missing operand (that we can abuse for number inputs as well).

    OPEN_BRACKET(0, "(") {
    @Override
        public BigDecimal evaluate(BigDecimal left, BigDecimal right) {
            throw new IllegalStateException("Cannot apply bracket operand to left and right numbers");
        }
    },
    CLOSE_BRACKET(0, ")") {
        @Override
        public BigDecimal evaluate(BigDecimal left, BigDecimal right) {
            throw new IllegalStateException("Cannot apply bracket operand to left and right numbers");
        }
    },
    NIL(0, "") {
        @Override
        public BigDecimal evaluate(BigDecimal left, BigDecimal right) {
            throw new IllegalStateException("trying to evaluate invalid operator");
        }
    };

updating the evaluateExpression method to work with Operators required a little bit of reordering to handle the numbers correctly again:

public static BigDecimal evaluateExpression(String infix)
{
    Stack<Operator> operatorStack = new Stack<>();
    Stack<BigDecimal> outputStack = new Stack<>();

    for (String currentToken : infix.split("\\s"))
    {
        Operator currentOperator = Operator.from(currentToken);
        if (currentOperator == Operator.OPEN_BRACKET)
        {
            operatorStack.push(currentOperator);
        } else if (currentOperator==Operator.CLOSE_BRACKET)
        {
            while (Operator.OPEN_BRACKET.equals(operatorStack.peek())) {
                Operator higherPrecedenceOperator = operatorStack.pop();
                BigDecimal operandLeft = outputStack.pop();
                BigDecimal operandRight = outputStack.pop();
                outputStack.push(higherPrecedenceOperator.evaluate(operandLeft, operandRight));
            }
            operatorStack.pop();
        } else if (currentOperator != Operator.NIL)
        {
            while (!operatorStack.isEmpty() && operatorStack.peek().isHigherPrecedence(currentOperator)) {
                Operator higherPrecedenceOperator = operatorStack.pop();
                BigDecimal operandLeft = outputStack.pop();
                BigDecimal operandRight = outputStack.pop();
                outputStack.push(higherPrecedenceOperator.evaluate(operandLeft, operandRight));
            }
            operatorStack.push(currentOperator);
        } else
        {
            outputStack.push(BigDecimal.valueOf(Integer.valueOf(currentToken)));
        }
    }

    while (!operatorStack.empty())
    {
        Operator higherPrecedenceOperator = operatorStack.pop();
        BigDecimal operandRight = outputStack.pop();
        BigDecimal operandLeft = outputStack.pop();
        outputStack.push(higherPrecedenceOperator.evaluate(operandLeft, operandRight));

    }
    return outputStack.pop();
}

This got me thinking. What do we have to change to allow the bracket operantors to just evaluate similarly to the other operators instead? We could first update the current evaluate method to take the outputStack instead of a left and right operand. That way the Operator can decide how many numbers it needs. Specifically for the brackets, we also need to pass in the Operator stack so that the opening bracket can just push itself on the stack, and the closing bracket can pop the stack untill it finds an opening bracket.

private enum Operator {
    ADD(1, "+") {
        @Override
        public void evaluate(Stack<BigDecimal> numbers, Stack<Operator> operatorStack) {
            BigDecimal right = numbers.pop();
            BigDecimal left = numbers.pop();
            numbers.push(left.add(right));
        }
    },
    SUBTRACT(2, "-") {
        @Override
        public void evaluate(Stack<BigDecimal> numbers, Stack<Operator> operatorStack) {
            BigDecimal right = numbers.pop();
            BigDecimal left = numbers.pop();
            numbers.push(left.subtract(right));
        }
    },
    MULTIPLY(3, "*") {
        @Override
        public void evaluate(Stack<BigDecimal> numbers, Stack<Operator> operatorStack) {
            BigDecimal right = numbers.pop();
            BigDecimal left = numbers.pop();
            numbers.push(left.multiply(right));
        }
    },
    DIVIDE(4, "/") {
        @Override
        public void evaluate(Stack<BigDecimal> numbers, Stack<Operator> operatorStack) {
            BigDecimal right = numbers.pop();
            BigDecimal left = numbers.pop();
            numbers.push(left.divide(right));
        }
    },
    OPEN_BRACKET(Integer.MIN_VALUE, "(") {
        @Override
        public void evaluate(Stack<BigDecimal> numbers, Stack<Operator> operatorStack) {
            operatorStack.push(this);
        }
    },
    CLOSE_BRACKET(Integer.MAX_VALUE, ")") {
        @Override
        public void evaluate(Stack<BigDecimal> numbers, Stack<Operator> operatorStack) {
            while (!operatorStack.isEmpty() && operatorStack.peek() != OPEN_BRACKET) {
                operatorStack.pop().evaluate(numbers, operatorStack);
            }
            if (operatorStack.isEmpty()) {
                //no open bracket found!
                throw new IllegalStateException("closing bracket requires earlier matching opening bracket");
            }
            operatorStack.pop();
        }
    },
    NIL(0, "") {
        @Override
        public void evaluate(Stack<BigDecimal> numbers, Stack<Operator> operatorStack) {
            throw new IllegalStateException("trying to evaluate invalid operator");
        }
    };

    private final int precedence;
    private final String sign;

    Operator(int precedence, String sign) {
        this.precedence = precedence;
        this.sign = sign;
    }

    public static Operator from(String oper) {
        for (Operator operator : Operator.values()) {
            if (operator.sign.equalsIgnoreCase(oper)) {
                return operator;
            }
        }
        return NIL;
    }

    public boolean isHigherPrecedence(Operator other) {
        return this.precedence <= other.precedence;
    }

    public abstract void evaluate(Stack<BigDecimal> numbers, Stack<Operator> operatorStack);
}

Quick note that I also updated the priority values for the open and close brackets so that they're handled correctly while going through the stack. This way we can greatly simplify the evaluateExpression implementation as well:

public static BigDecimal evaluateExpression(String infix) {
    Stack<Operator> operatorStack = new Stack<>();
    Stack<BigDecimal> outputStack = new Stack<>();

    for (String currentToken : infix.split("\\s")) {
        Operator currentOperator = Operator.from(currentToken);
        if (currentOperator == Operator.NIL) { //number (or error from unknown operator?)
            outputStack.push(BigDecimal.valueOf(Integer.valueOf(currentToken)));
        } else {
            while (!operatorStack.isEmpty() && !operatorStack.peek().isHigherPrecedence(currentOperator)) {
                Operator higherPrecedenceOperator = operatorStack.pop();
                higherPrecedenceOperator.evaluate(outputStack, operatorStack);
            }
            operatorStack.push(currentOperator);
        }
    }

    while (!operatorStack.empty()) {
        operatorStack.pop().evaluate(outputStack, operatorStack);
    }
    return outputStack.pop();
}

The best part about implementing the Operators this way? We can easily add new operators, and they're not limited to 2 operands either! For example, adding the following Operand to the enum class:

    ABS(5, "abs") {
        @Override
        public void evaluate(Stack<BigDecimal> numbers, Stack<Operator> operatorStack) {
            numbers.push(numbers.pop().abs());
        }
    },

is all we need to do to then input things like "abs -2" which gets resolved to "2".


Binary tree inorder traversal without stack or recursion I was looking for ways to traverse a binary tree and came across things like Morris Traversal. Except I don't like the idea of modyfying my tree, nor use stack or recursion. Not finding other solution, I wrote my own algorithm. Basically it's depth-first search, but every node has counter of type integer indicating how many times the method traversed this node. Given every node has a parent and two children, this counter takes values from 0 to 2. Therefore the method executes 3 times for every node. Here's the code:

Node root;

public void Traverse() {
    Node node = root;
    while(node != null) {
        node = Get(node);
    }
}

Node Get(Node node) {
    Node child;
    if(node.Counter == 0) {
        child = node.Left;
    }
    else if(node.Counter == 1) {
        child = node.Right;
        System.Console.WriteLine(node);//display node
    }
    else {
        node.Counter = 0;//reset counter
        if(node.Parent != null) {
            node.Parent.Counter++;
            return node.Parent;
        }
        else {
            return null;
        }
    }
    if(child == null) {
        node.Counter++;
        return node;
    }
    return child;
}

My question: in what terms if any, this code is better than Morris traversal or using stack?

Comparison

I don't think this approach offers any benefits compared to Morris traversal, a stack-based or a recursive approach:

Both this and Morris are modifying the given tree, which I consider to be a negative thing. You're not rearranging nodes like Morris does, but those counter fields still prevent simultaneous traversals. A recursive or stack-based approach does not have that limitation.
Adding an additional field to your Node class is 'intrusive', and it increases memory use even when you never traverse a tree.
Unlike the other approaches, yours requires nodes to have a reference to their parent.
In all the tests I've done (using trees of 100 - 100K nodes) this was consistently slower than Morris, which itself was slower than a recursive or stack-based approach.
In terms of how easy the code is to understand, I'd say it's similar to Morris, but both are more complicated than a recursive and stack-based approach.

I don't know why you don't like the idea of using recursion. It's actually a very natural approach when working with tree-like structures. It's easy to implement, fast, and doesn't modify the tree or require structural changes.

public void Traverse(Node node, Action<Node> visit)
{
    visit(node);
    if (node.Left != null)
        Traverse(node.Left, visit);
    if (node.Right != null)
        Traverse(node.Right, visit);
}

With recursion there's always a risk of stack overflow, but that should only be a concern when working with highly imbalanced trees.

Improved approach

Your approach can be modified so it no longer needs those counter fields. You've got the following states:

node.Counter == 0: Move to the left child.
node.Counter == 1: Move to the right child.
node.Counter == 2: Move back to the parent.

But you can also distinguish between these states if you only keep track of the previously visited node:

previous == current.Parent: Move to the left child.
previous == current.Left: Move to the right child.
previous == current.Right: Move back to the parent.

In terms of performance it's somewhere in-between recursive and Morris. It can also be generalized for nodes with a variable number of children. Still, it remains a relatively complicated approach.

Other notes

The root node should be passed to Traverse as an argument, not via a 'global' variable.
Get is a very undescriptive name. GetNextNode sounds better, except that it sometimes returns the same node (in a different state), so that name is slightly misleading. Maybe ContinueTraversal?
Get is only useful within the context of Traverse, so it can be made a local function.
Those counter values indicate specific states, so I'd use an enum instead of 'magic numbers'. Alternately, you could rename it to something like visitedChildNodeCount.
Hardcoding System.Console.WriteLine(node) isn't very flexible. Consider passing in the action to be performed as an Action<Node> argument.
Instead of writing if (condition) { ... } else { return null; }, I'd invert that to if (!condition) return null; ... to reduce nesting.


Fixed point iteration and cobweb plot I'm using Python to find fixed points of a given function and then draw a cobweb plot to visualize it. Thanks to this question, I have the core of the code written and can accomplish the task, but I have a number of questions about improving the functionality of the code.

import numpy as np
import matplotlib.pyplot as plt

def f(x):
    return 2*x*(1-x)

class fixpt(object):

    def __init__(self, x):
        self.x = x
        self.f = f

    def search(self, N = 1000, epsilon = 1.0E-100, store = False):
        x = self.x
        y = self.f(x)
        n = 0
        if store: values = [(x,y)]
        while abs(y-x) >= epsilon and n < N:
            x = f(x)
            n += 1
            y = f(x)
            if store: values.append((x,y))
        if store:
            return y, values
        else:
            if n >= N:
                return "No fixed point for given initial condition"
            else:
                return x, n, y
    def plot(self):
        res, points = self.search(store = True)
        xaxis = np.linspace(0,1,1000)
        plt.plot(xaxis, f(xaxis), 'b')
        plt.plot(xaxis, xaxis, 'r')

        for x, y in points:
            plt.plot([x, x], [x, y], 'g')
            plt.plot([x, y], [y, y], 'g')

        plt.show()

What I'm wondering is how I can:

Avoid the need to redefine the function f(x) when analyzing other functions (by, perhaps asking for user input)
Avoid the need to change the range of concern (i.e. the range in np.linspace) in a similar manner
Improve the structure of this code in general (I'm a Python noob and get the feeling I've created a class for little to no reason
Most importantly: use this code iteratively to scan for fixed points for all x in a given range

Your questions

Functions can be function arguments, too.
You can add a parameter for that.
See additional comments. I think it's fine that you use a class.
That is usually not in the range of codereview. That should be asked in StackOverflow (but I think I know what you want - just look at the code).

Additional comments

Add a shebang. If you do, you can directly execute the script. See What does #!/usr/bin/python mean?
Read PEP8 or at least use pep8online.com to make your coding style standard conform and hence easier to read. It's mostly about setting whitespaces correct.
Use docstrings and eventually doctest. That makes your code again easier to read and you can easily verify if it works as you expect.
If you don't use a variable, you should remove it at all. If that is not possible (e.g. in the case of unzipping values) you should name the variable _.

The code

#!/usr/bin/env python

import numpy as np
import matplotlib.pyplot as plt

def f(x):
    """An (arbitrary) quadratic mathematical function.
       It has fixpoints 0 and 0.5.

    >>> f(0)
    0
    >>> f(0.5)
    0.5
    """
    return 2*x*(1-x)

def g(x):
    """An (arbitrary) quadratic mathematical function.
       It has fixpoints 0 and (+/-) 1/sqrt(2)

    >>> g(0)
    0
    >>> "%0.4f" % g(-0.7071)
    '-0.7071'
    """
    return 2*x*(1-x)*(1+x)

class fixpt(object):
    """Find fixed points of a given function f."""
    def __init__(self, x, f, xmin=0, xmax=1, epsilon=1.0E-100):
        """Constructor
        @param x float - where to start search
        @param f function - function that should get evaluated
        @param xmin float - Interval that gets examined
        @param xmax float - Interval that gets examined
        @param epsilon float - comparison when floats are considered to be
                               the same
        """
        self.x = x
        self.f = f
        self.xmin = xmin
        self.xmax = xmax
        self.epsilon = epsilon

    def search(self, N=1000, store=False):
        """Search fixed points.
        @param N int - number of iterations
        @param store - bool - if set, return ????
        @return I did not understand that. It seems to depend on 'store'

        >>> fixpointiter = fixpt(0, f)
        >>> fixpointiter.search()
        (0, 0, 0)
        """
        # Set initial values
        x = self.x
        y = self.f(x)
        n = 0
        if store:
            values = [(x, y)]  # Why?
        while abs(y-x) >= self.epsilon and n < N:
            x = f(x)
            n += 1
            y = f(x)
            if store:
                values.append((x, y))
        if store:
            return y, values
        else:
            if n >= N or x == -float('inf') or x == float('inf'):
                # No fixed point for given initial condition
                return None
            else:
                return x, n, y

    def plot(self):
        """Draw a cobweb plot."""
        _, points = self.search(store=True)
        xaxis = np.linspace(self.xmin, self.xmax, 1000)
        plt.plot(xaxis, f(xaxis), 'b')
        plt.plot(xaxis, xaxis, 'r')

        for x, y in points:
            plt.plot([x, x], [x, y], 'g')
            plt.plot([x, y], [y, y], 'g')

        plt.show()

    def scan(self):
        """Scan for all fix points in xmin / xmax."""
        x_range = np.linspace(self.xmin, self.xmax, 1000)
        fixpoints = []
        for x in x_range:
            self.x = x
            ret = self.search()
            if ret is not None:
                fixpoints.append(ret[0])
        # Get rid of duplicates
        fixpoints = sorted(fixpoints)
        final = []
        for x in fixpoints:
            # It's not the same as the last fixpoint
            if len(final) == 0 or abs(final[-1]-x) >= self.epsilon:
                final.append(x)
        return final

if __name__ == '__main__':
    # Automatic doctest
    import doctest
    doctest.testmod()
    # Usage example
    fixpointiter = fixpt(0, g, -2, 2)
    print(fixpointiter.search())
    print(fixpointiter.search(store=True))
    print(fixpointiter.scan())


Displaying a CSV file as an HTML table in a CRUD page <?php
            // Read all lines of the CSV file into an array
            // The "file" function in PHP returns an array of all the lines in the file listed
            $lines = file('passes.csv',FILE_IGNORE_NEW_LINES);

            // Counter variable for line number
            $i = 0;

            //Iterate over the array of lines
            foreach($lines as $line) {
                $parts = explode(',',$line);
                $type = $parts[0];
                $site_name = $parts[1];
                $site_address = $parts[2];
                $contact_no = $parts[3];
                $customer_no = $parts[4];
                $details = $parts[5];
                $email = $parts[6];
                $phone = $parts[7];
                $tech = $parts[8];
                echo '
';
                echo    "$type	";
                echo    "$site_name	";
                echo    "$site_address	";
                echo    "$contact_no	";
                echo    "$customer_no	";
                echo    "$details	";
                echo    "$email	";
                echo    "$phone	";
                echo    "$tech	";
                echo    " 	";
                echo '
';

                $i++; // increment line number
            }
            ?>

This is slow. also it would be awesome if someone could tell me how to do this dynamically with different csv file.

The biggest problem with this code is not so much the CSV formatting, but the fact that you have a "Delete" link like this:

<a class="btn btn-danger" href="actions/delete_band.php?linenum=$i">

The fact that a deletion can be triggered by an HTTP GET request exposes you to a Spider of Doom situation:

  Things went pretty well for a few days after going live. But, on day six, things went not-so-well: all of the content on the website had completely vanished and all pages led to the default "please enter content" page. […]

  A user [had] copied and pasted some content from one page to another, including an "edit" hyperlink to edit the content on the page. Normally, this wouldn't be an issue, since an outside user would need to enter a name and password. But, the CMS authentication subsystem didn't take into account the sophisticated hacking techniques of Google's spider. Whoops.

Moreover, GET requests are generally cacheable, so clicking on the link might not reliably cause the row to be deleted.

For requests that trigger a state change on the server, what you want is an HTTP POST.  POSTs are not cacheable, and will not be triggered by web crawlers.  To change the link to perform a POST, you could either make it a button in a form or do something using JavaScript.


Google Analytic Webcrawler for every country code At the moment I'd like recommendation where I can change and what I can use to improve on simplicity and modularity. If I'm practicing good naming conventions as to provide clean readable code. Any critique is much appreciated.

import time
import re
import requests
from bs4 import BeautifulSoup, SoupStrainer
import pandas as pd
import time

SESSION = requests.Session()

""" This is the Google Analytics Selector Section """

class myGoogleSession:

    def fetch_google_xml(self, URL, country_code):
        format_url = f"{URL}{country_code}"
        response = SESSION.get(format_url)
        soup = BeautifulSoup(response.text, 'xml',
                             parse_only=SoupStrainer('channel'))
        return soup

google_session = myGoogleSession()

def google_trends_retriever(URL, country_code):
    xml_soup = google_session.fetch_google_xml(URL, country_code)
    print(country_code)
    return[(title.text, re.sub("[+,]", "", traffic.text))
           for title, traffic in zip(xml_soup.find_all('title')[1:],
                                     xml_soup.find_all('ht:approx_traffic'))]

def create_pdTrend(data):
    check_panda = pd.DataFrame(
        google_trends_retriever(GoogleURL, data),
        columns=['Title', 'Score']
    )
    if len(check_panda) == 0:
        print('No available data')
    else:
        return check_panda

""" This is the Country Code Selector Section """

country_code_list = []

class myCountryCodeSession:
    def fetch_countrycode_html(self, URL):
        response = SESSION.get(URL)
        soup = BeautifulSoup(response.text, 'html.parser',
                             parse_only=SoupStrainer('table'))
        return soup

countryCode_session = myCountryCodeSession()

def parse_row(url):
    rows = countryCode_session.fetch_countrycode_html(url)
    _rows = rows.findChildren(['td', 'tr'])
    for row in _rows:
        cells = row.findChildren('td')[2:3]
        for cell in cells:
            value = cell.string
            country_code_list.append(value[:2])
    return None

def create_pdCountryCode(country_code):
    return pd.DataFrame({'Country_Code': country_code})

def iterate_List(data):
    i = 1
    while i <= 239:
        selected_CountryCode = get_data_fromList(i)
        print(create_pdTrend(selected_CountryCode))
        i += 1
    else:
        print('Has reach the end of i ' + str(i))

def get_data_fromList(num):
    key = num-1
    for i in country_code_list[key:num]:
        return str(i)

if __name__ == '__main__':
    """ URL Section """
    GoogleURL = "https://trends.google.com/trends/trendingsearches/daily/rss?geo="
    CountryCodeURL = "https://countrycode.org/"
    """-------------"""
    start = time.time()
    print("hello")

    """Country Code Section """
    parse_row(CountryCodeURL)
    """---------------------"""

    """Google Analytics Section """
    iterate_List(country_code_list)
    """-------------------------"""
    end = time.time()
    print(end - start)

PEP8

This is the official Python style guide. If you are interested in good naming conventions and other good practices, you can start here.

Among other things, you code would benefit from:

variable name using lower_snake_case;
class name using PascalCase;
comments delimited using # and not raw strings in the code;
redundant/useless parts of the code removed.

A first rewrite would yield:

import re
import time

import requests
import pandas as pd
from bs4 import BeautifulSoup, SoupStrainer

SESSION = requests.Session()

# This is the Google Analytics Selector Section
class GoogleSession:
    def fetch_google_xml(self, URL, country_code):
        response = SESSION.get(f"{URL}{country_code}")
        return BeautifulSoup(
                response.text, 'xml',
                parse_only=SoupStrainer('channel'))

google_session = GoogleSession()

def google_trends_retriever(URL, country_code):
    xml_soup = google_session.fetch_google_xml(URL, country_code)
    print(country_code)
    titles = xml_soup.find_all('title')[1:]
    traffics = xml_soup.find_all('ht:approx_traffic')
    return [
            (title.text, re.sub("[+,]", "", traffic.text))
            for title, traffic in zip(titles, traffics)
    ]

def create_pd_trend(data):
    check_panda = pd.DataFrame(
            google_trends_retriever(google_URL, data),
            columns=['Title', 'Score'],
    )
    if len(check_panda) == 0:
        print('No available data')
    else:
        return check_panda

# This is the Country Code Selector Section
country_code_list = []

class CountryCodeSession:
    def fetch_country_code_html(self, URL):
        response = SESSION.get(URL)
        return BeautifulSoup(
                response.text, 'html.parser',
                parse_only=SoupStrainer('table'))

country_code_session = CountryCodeSession()

def parse_row(url):
    rows = country_code_session.fetch_country_code_html(url)
    for row in rows.find_all(['td', 'tr']):
        cells = row.find_all('td')[2:3]
        for cell in cells:
            value = cell.string
            country_code_list.append(value[:2])

def iterate_list(data):
    i = 1
    while i <= 239:
        selected_country_code = get_data_from_list(i)
        print(create_pd_trend(selected_country_code))
        i += 1
    else:
        print('Has reach the end of i', i)

def get_data_from_list(num):
    key = num - 1
    for i in country_code_list[key:num]:
        return str(i)

if __name__ == '__main__':
    # URL Section
    google_URL = "https://trends.google.com/trends/trendingsearches/daily/rss?geo="
    country_code_URL = "https://countrycode.org/"
    # -------------
    start = time.time()
    print("hello")

    # Country Code Section
    parse_row(country_code_URL)
    # ---------------------

    # Google Analytics Section
    iterate_list(country_code_list)
    # -------------------------
    end = time.time()
    print(end - start)

Loop like a native

When I saw

def get_data_fromList(num):
    key = num-1
    for i in country_code_list[key:num]:
        return str(i)

I wondered why you would write such convoluted code. Extracting a sublist of one element to iterate over it and return the first one… You can simplify that to

def get_data_from_list(num):
    return str(country_code_list[num - 1])

But I wondered why using a method for that, and saw how you iterated over indices to call this function. Don't. Use a for-loop as it is meant to be used: by iterating over the content directly.

This would yield:

import re
import time

import requests
import pandas as pd
from bs4 import BeautifulSoup, SoupStrainer

SESSION = requests.Session()

# This is the Google Analytics Selector Section
class GoogleSession:
    def fetch_google_xml(self, URL, country_code):
        response = SESSION.get(f"{URL}{country_code}")
        return BeautifulSoup(
                response.text, 'xml',
                parse_only=SoupStrainer('channel'))

google_session = GoogleSession()

def google_trends_retriever(URL, country_code):
    xml_soup = google_session.fetch_google_xml(URL, country_code)
    print(country_code)
    titles = xml_soup.find_all('title')[1:]
    traffics = xml_soup.find_all('ht:approx_traffic')
    return [
            (title.text, re.sub("[+,]", "", traffic.text))
            for title, traffic in zip(titles, traffics)
    ]

def create_pd_trend(data):
    check_panda = pd.DataFrame(
            google_trends_retriever(google_URL, data),
            columns=['Title', 'Score'],
    )
    if len(check_panda) == 0:
        print('No available data')
    else:
        return check_panda

# This is the Country Code Selector Section
class CountryCodeSession:
    def fetch_country_code_html(self, URL):
        response = SESSION.get(URL)
        return BeautifulSoup(
                response.text, 'html.parser',
                parse_only=SoupStrainer('table'))

country_code_session = CountryCodeSession()

def parse_row(url):
    rows = country_code_session.fetch_country_code_html(url)
    return [
            cell.string[:2]
            for row in rows.find_all(['td', 'tr'])
            for cell in row.find_all('td')[2:3]
    ]

def iterate_list(country_codes):
    for country_code in country_codes:
        print(create_pd_trend(str(country_code)))
    else:
        print('Has reach the end of i', len(country_codes))

if __name__ == '__main__':
    # URL Section
    google_URL = "https://trends.google.com/trends/trendingsearches/daily/rss?geo="
    country_code_URL = "https://countrycode.org/"
    # -------------
    start = time.time()
    print("hello")

    # Country Code Section
    country_code_list = parse_row(country_code_URL)
    # ---------------------

    # Google Analytics Section
    iterate_list(country_code_list)
    # -------------------------
    end = time.time()
    print(end - start)

Stop writting classes

Your classes add absolutely no value over a single function. You don't store state that you reuse after each call. You don't share state between several functions. They are plain functions in a namespace, just let them be plain functions.

This code can benefit from using a class, but not like that.

Parse bytes, not text

lxml, which is the underlying parser used when instructing BeautifulSoup to decode 'xml' explicitly works with raw bytes rather than decoded text. This is to be able to detect explicit encoding declarations and decode the rest of the document appropriately; so you will never have decoding errors.

This means that you need to feed the response.content rather than response.text to BeautifulSoup when parsing XML.

Manage your state properly

Your code heavily relly on global variables and printing data to work. This is the worst part of your code as it make it barely reusable and harder to test properly (think unittest or doctest).

Instead of using global variables, pass them around as parameters and return them from your functions.

Instead of printing results, return values from your functions. This make it easier to extract and massage data into your liking.

There is also the global SESSION that is used throughout the code. I'd encapsulate that into a class to have a single session per instance so you can easily crawl several addresses if need be.

My take on the problem would be:

import re
from functools import partial

import requests
import pandas as pd
from bs4 import BeautifulSoup, SoupStrainer

class GoogleAnalysis:
    def __init__(self, url):
        session = requests.Session()
        self.get_url = partial(session.get, url)

    def _fetch_xml(self, country_code):
        response = self.get_url(params={'geo': country_code})
        return BeautifulSoup(
                response.content, 'xml',
                parse_only=SoupStrainer('channel'))

    def _retrieve_trends(self, country_code):
        soup = self._fetch_xml(country_code)
        titles = soup.find_all('title')[1:]
        traffics = soup.find_all('ht:approx_traffic')
        return [
                (title.text, re.sub("[+,]", "", traffic.text))
                for title, traffic in zip(titles, traffics)
        ]

    def trends(self, country_code):
        df = pd.DataFrame(
                self._retrieve_trends(country_code),
                columns=['Title', 'Score'],
        )
        df['Country Code'] = country_code
        return df

def country_codes(url='https://countrycode.org/'):
    response = requests.get(url)
    soup = BeautifulSoup(
            response.text, 'lxml',
            parse_only=SoupStrainer('table'))
    return [
            cell.string[:2]
            for row in soup.find_all(['td', 'tr'])
            # Some rows don't define row.find_all('td')[2] so filter out
            for cell in row.find_all('td')[2:3]
    ]

def main(url):
    google = GoogleAnalysis(url)
    codes = country_codes()
    return pd.concat([
        google.trends(country_code)
        # Country codes are repeated twice, we only need them once
        for country_code in codes[:len(codes) // 2]
    ])

if __name__ == '__main__':
    import time
    start = time.perf_counter()
    print('Hello!')
    trends = main('https://trends.google.com/trends/trendingsearches/daily/rss')
    print(trends.to_string(index=False))
    print(time.perf_counter() - start)

Note the print(trends.to_string(index=False)) at the end, this could be whatever you like, either printing to CSV or using trends.groupby to redo your old formatting. The idea here is that the computation is done without printing anything. You get to format the data at the end however you like.


Validating Array before rendering I am rendering a list from an array of objects. I want to check the correctness of the array before rendering. For that I am using typeof and null.

In real situation the list comes from the API.

Is it the correct way? and is there any better way to do it?

const activeEvents = [{
  id: '1',
  title: 'event 1'
}, {
  id: '2',
  title: 'event 2'
}]

function App() {
  return (

 List

      { typeof activeEvents === 'object' &&
      activeEvents !== null &&
      activeEvents.map(event => (

 { event.title }

       ))}

);
}

ReactDOM.render( <
  App / > ,
  document.getElementById('root')
);

typeof activeEvents === 'object' && activeEvents !== null can be true for both Array and Object :

const array = [];
const obj = {};

console.log(typeof array === 'object' && array !== null)
console.log(typeof obj === 'object' && obj !== null)

you can use Array.isArray

const activeEvents = [{
  id: '1',
  title: 'event 1'
}, {
  id: '2',
  title: 'event 2'
}]

function App() {
  return (

 List

      { Array.isArray(activeEvents) &&
        activeEvents.map(event => (

 { event.title }

        ))
      }

  );
}

ReactDOM.render( <
  App / > ,
  document.getElementById('root')
);


Maximum number of values in an array I have an array to have a maximum of 10 elements. Whenever I add a new element, if it already has 10, it would remove the first and push a new one.

I made this:

Array.prototype.pushMax = function(value,max){

     if (this.length >= max){
           this.shift();
     }
     this.push(value);

}

Which seems to work fine, however, I'm just wondering if there is a better way to do it (perhaps some native JavaScript function)

There is no native way to do this so your general logic seems sound.  Here's some improvements, you might consider:

Make sure the pushMax function is not enumerable so people who iterate arrays with for (var index in array) won't see the pushMax property.
Ensure the array stays below max length even if it was previously above max
Return the new length of the array, the same as .push():

Here's a working snippet:

Object.defineProperty(Array.prototype, "pushMax", {
    configurable: false,
    enumerable: false,
    writable: false,
    value: function(value, max) {
        if (this.length >= max) {
           this.splice(0, this.length - max + 1);
      }
      return this.push(value);
    }
});

var x = [0,1,2,3,4,5,6,7,8,9,10];
x.pushMax(11, 10);
x.pushMax(12, 10);
x.pushMax(13, 10);

// wrong way to iterate an array, but just testing enumerability
for (var i in x) {
    document.write(i + ": " +  x[i] + "<br>");
}

You could also consider making the max length be a non-enumerable property of the array so you don't have to pass it to .pushMax() every time.

I'd really rather see this in a subclass of an Array rather than modifying the built-in Array object, but alas we can't subclass the Array object yet in most browsers.


Create a summary report by summing the amount's of accounts with the same name and within the date range specified I have heard from my friend that it is bad practice to normally loop though the whole database to meet certain criteria. He mentioned something about the proper way being that you index the objects of interest.

What I want to achieve here is make a report for our company. So I do this by summing up all the items that have the same account that is within the filter date range set by start_date and end_date to start.

Some variable definitions:

Account name: data_entries.iloc[j, 4]

Account type: data_listofaccounts.iloc[i, 1]

Account amount: data_entries.iloc[j, 5]

So is there a more efficient way to write this code that will be less computationally taxing on the computer specifically. (minimize computational requirement)

import pandas as pd
import datetime

entries_csv = "C:\\Users\\Pops\\Desktop\\Entries.csv"
listofaccounts_csv = "C:\\Users\\Pops\\Desktop\\List of Accounts.csv"

data_entries = pd.read_csv(entries_csv)
data_listofaccounts = pd.read_csv(listofaccounts_csv)
data_entries['VOUCHER DATE'] = pd.to_datetime(data_entries['VOUCHER DATE'], format="%m/%d/%Y").dt.date

summary_amount = [0]*(len(data_listofaccounts) + 1)
summary = (('DEBIT ACCOUNT', 'DEBIT AMOUNT'),)

start_date = datetime.date(2018, 4, 1)
end_date = datetime.date(2018, 10, 30)

for i in range(0, len(data_listofaccounts)):
    for j in range(0, len(data_entries)):
        if start_date <= data_entries.iloc[j, 1] <= end_date:
            if data_listofaccounts.iloc[i, 0] == data_entries.iloc[j, 4]\
                    and (data_listofaccounts.iloc[i, 1] == "CURRENT ASSET" or
                         data_listofaccounts.iloc[i, 1] == "FIXED ASSET" or
                         data_listofaccounts.iloc[i, 1] == "EXPENSE"):
                summary_amount[i] += data_entries.iloc[j, 5]
            elif data_listofaccounts.iloc[i, 0] == data_entries.iloc[j, 4]\
                    and (data_listofaccounts.iloc[i, 1] == "CURRENT LIABILITY" or
                         data_listofaccounts.iloc[i, 1] == "LONG TERM LIABILITY" or
                         data_listofaccounts.iloc[i, 1] == "EQUITY"):
                summary_amount[i] -= data_entries.iloc[j, 5]
    summary += ((data_listofaccounts.iloc[i, 0], "{:,}".format(round(summary_amount[i], 2))),)

Entries sample data:

List of Accounts sample data:

List of Accounts contains unique account names while in the Entries worksheet, it can be repeated.

The kind of operation you’re doing is called a join: you want to associate data from a DataFrame to data from another one based on a shared information on a given column.

To join a DataFrame to another one or to a Series, you need to respect a simple rule: either you join on index or a column is joined to an index; and they must be of similar nature. So in your case, since you need to join on the name of the account, one of your DataFrame must be indexed by this name. Since it is its purpose, you need to reindex data_listofaccounts by its 'Account Name' column:

data_listofaccounts = pd.read_csv(listofaccounts_csv)
data_listofaccounts = data_listofaccounts.set_index(['Account Name'])

Then, before joining, you can filter out data that is out of your study range so the join is performed on less data:

filtered = data_entries[(start_date <= data_entries['VOUCHER DATE']) & (data_entries['VOUCHER DATE'] <= end_date)]

And thus the data you’re interested in is accessed using:

data_entries = pd.read_csv(entries_csv)
data_entries['VOUCHER DATE'] = pd.to_datetime(data_entries['VOUCHER DATE'], format="%m/%d/%Y")
data_listofaccounts = pd.read_csv(listofaccounts_csv)
data_listofaccounts = data_listofaccounts.set_index(['Account Name'])

start_date = datetime.date(2018, 4, 1)
end_date = datetime.date(2018, 10, 30)
date_mask = (start_date <= data_entries['VOUCHER DATE']) & (data_entries['VOUCHER DATE'] <= end_date)
interesting = data_entries[date_mask].join(data_listofaccounts, on='DEBIT ACCOUNT')

And then each row of interesting will have all the information needed: the transaction date, the name of the account, its type and the amount spent.

But this is all without taking into account the kind of operations you want to perform afterwards: grouping by name and summing the amounts. You can perform this operation directly before joining and it will simplify the process altogether:

data_entries = pd.read_csv(entries_csv)
data_entries['VOUCHER DATE'] = pd.to_datetime(data_entries['VOUCHER DATE'], format="%m/%d/%Y")

start_date = datetime.date(2018, 4, 1)
end_date = datetime.date(2018, 10, 30)
date_mask = (start_date <= data_entries['VOUCHER DATE']) & (data_entries['VOUCHER DATE'] <= end_date)

amount_per_account = data_entries[date_mask].groupby(['DEBIT ACCOUNT']).sum()

This will return a DataFrame indexed by the accounts names whose 'DEBIT AMOUNT' column is the sum of each row pertaining to this account. You then just need to join with data_listofaccounts to know if this sum should be positive or negative based on the 'PARENT NODE' column.

summary = data_listofaccounts.join(amount_per_account, on='Account Name', how='outer').fillna(0)
debit_mask = (summary.Type == 'CURRENT LIABILITY') | (summary.Type == 'LONG TERM LIABILITY') | (summary.Type == 'EQUITY')
summary[debit_mask]['DEBIT AMOUNT'] = -summary[debit_mask]['DEBIT AMOUNT']

Other improvements pertaining to coding style:

you should define functions to organize your code
you should guard your code using if __name__ == '__main__'
you don't need to say that a variable contain some data_; same for namming a collection, you don't need to say what kind of collection hold the data (besides, in your case it is misleading as your listofaccounts is in fact a DataFrame); so data_listofaccounts => accounts
you should follow PEP8 namming conventions

And to pandas:

you can limit the amount of data retrieved from your CSVs by using the usecols argument; this will lead to less data manipulation afterwards and thus more speed.


Adjacency list with array data I came through a question on Stack Overflow, which shows this array structure:

array (size=4)
  0 =>
    array (size=3)
      'id' => int 1
      'name' => string 'Anniversary' (length=11)
      'parent' => int 0
  1 =>
    array (size=3)
      'id' => int 12
      'name' => string 'New arrives' (length=11)
      'parent' => int 1
  2 =>
    array (size=3)
      'id' => int 13
      'name' => string 'Discount' (length=8)
      'parent' => int 12
  3 =>
    array (size=3)
      'id' => int 6
      'name' => string 'Birthday' (length=8)
      'parent' => int 0

and the required outcome needs to be the path for that list like shown below, implementation of adjacency list:

13 - Anniversary->New arrives->Discount
12 - Anniversary->New arrives
1 - Anniversary
6 - Birthday

I came to this solution:

$in = [
    ['id'=>1, 'name'=>'Anniversary', 'parent'=>0],
    ['id'=>12, 'name'=>'New arrives', 'parent'=>1],
    ['id'=>13, 'name'=>'Discount', 'parent'=>12 ],
    ['id'=>6, 'name'=>'Birthday', 'parent'=>0 ]
];

function path($element, $input) {
    $return = $input[ array_search($element, array_column($input, 'id')) ];
    $str [] = $return ['name'];
    if($return ['parent'] != 0) {
        $str [] = path($return ['parent'], $input);
    }
    return implode('->', array_reverse($str));
}

var_dump(path(13,$in));

It gets the task done, but I want to know if any improvements can be made to optimize the approach to the problem.

Your code is really good, so there's not much to improve on, that I can see, but nonetheless:

You shouldn't have whitespace before, or inside your brackets unless required:

   $return = $input[ array_search($element, array_column($input, 'id')) ];
                    ^                                                  ^
   $str [] = $return ['name'];
       ^            ^
   if($return ['parent'] != 0) {
             ^
       $str [] = path($return ['parent'], $input);
           ^                 ^

All these lines above have unnecessary whitespace, however the line below is missing whitespace after the comma in path().

var_dump(path(13,$in));
                 ^

You really ought not to name your variables $return or $str, because the first is a system keyword, and the second is undescriptive & possibly confusing as it's really an array, not a string.

The implode string -> could also be -> with an extra space, or even a unicode long (or not long) rightwards arrow: ⟶, along with the extra space, visually output would be more readable, and better looking.

13 - Anniversary -> New arrives -> Discount // With added space
13 - Anniversary ⟶ New arrives ⟶ Discount // With added space and long rightwards arrow


Clockwise rotate a matrix in Scala Given a matrix, clockwise rotate elements in it.
Examples:

Input
1    2    3
4    5    6
7    8    9

Output:
4    1    2
7    5    3
8    9    6

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

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

Scala Implementation is below. I do feel that I do not need to have a rotatedMatrix predefined (which I am filling with -1) and I can build it up during my recursive calls, but I am not able to do it.
Thanks in advance for helping me on this.

import scala.util.Random

object MatrixRotate extends App {

  def getCol(c: Int, m: Array[Array[Int]]): Seq[Int] = (0 to (m(0).length - 1)) map ( r => m(r)(c))
  def getRow(r: Int, m: Array[Array[Int]]): Seq[Int] = m(r).toSeq
  def innerMatrix(m: Array[Array[Int]]): Array[Array[Int]] = {
    if (m.length == 0) Array.empty
    else {
      val rows = m.length - 1
      val cols = m(0).length - 1

      (1 until rows).map { r =>
        (1 until cols).map { c =>
          m(r)(c)
        }.toArray
      }.toArray
    }
  }

  val rotatedMatrix = Array.fill(6)(Array.fill(6)(-1))
  def rotateByOne(m: Array[Array[Int]], startRow: Int, startCol: Int,
                  endRow: Int, endCol: Int): Unit = {
    m.size match {
      case 0 =>
      case _ =>
        getRow(0, m) match {
          case Nil =>
          case x => x.init.zipWithIndex foreach { case (e, index) => rotatedMatrix(startRow)(startCol+index+1) = e }
        }
        getCol(m(0).length - 1, m) match {
          case Nil =>
          case x => x.init.zipWithIndex foreach { case (e, index) =>  rotatedMatrix(startRow+index+1)(endCol) = e }
        }
        getRow(m.length - 1, m) match {
          case x :: Nil =>
          case x => x.tail.zipWithIndex foreach { case (e, index) => rotatedMatrix(endRow)(startCol+index) = e }
        }
        getCol(0, m) match {
          case x :: Nil =>
          case x => x.tail.zipWithIndex foreach { case (e, index) => rotatedMatrix(startRow+index)(startCol) = e }
        }
        rotateByOne(innerMatrix(m), startRow + 1, startCol + 1, endRow - 1, endCol - 1)
    }

  }

  val matrix = Array.fill(6)(Array.fill(6)(Random.nextInt(100)))
  matrix foreach(row => println(row.mkString(",")))

  println("----------")

  rotateByOne(matrix, 0, 0, 5, 5)
  rotatedMatrix foreach (row => println(row.mkString(",")))
}

Update

I realize that this code seems to be failing for rectangular matrix. I still need to solve it for that case.

Functional programming

Since you've tagged this post with functional-programming I guess that the goal of this exercise may have been to practice functional programming or to present you skills in functional programming. This way or another, I'll stick to the assumption that it was supposed to be written in the functional programming paradigm.

When working with functional code, a function returning a Unit is a huge warning sign. In functional programming we compose programs of functions which are (quoting John De Goes):

  Total: They return an output for every input.
  Deterministic: They return the same output for the same input.
  Pure: Their only effect is computing the output.

In FP, if a function returns a Unit, it basically means that the function does nothing, as the only thing a function can do is to return a result.

rotatedMatrix

In FP preallocating the rotatedMatrix doesn't make sens, because in order to stay Pure rotateByOne is disallowed to mutate the rotatedMatrix. And the solution is simple, the rotateByOne should allocate and return a rotated matrix.

rotateByOne

While moving the allocation of rotatedMatrix into the rotateByOne would make rotateByOne a pure function, the implementation of the function would still be clattered with state mutations, and generally much more complex than it needs to be.

So let's have a look at how it can be improved.

Edge-case/error handling aside, I would expect the problem to be solved with a def rotateByOne(in: Array[Array[Int]]): Array[Array[Int]]. That function would have to create a new matrix of the same dimensions, finding a new value of each cell in the matrix. It's implementation could look like

  def rotateByOne(in: Array[Array[Int]]): Array[Array[Int]] = {
    val size = in.length
    (0 until size).map { i =>
      (0 until size).map { j =>
        newValueAt(in, i, j)
      }.toArray
    }.toArray
  }

Now all that's left is to implement the def newValueAt(in: Array[Array[Int]], i: Int, j: Int): Int which is again - a pure function - doing nothing but returning an Int. There are 5 cases to be considered, in newValueAt. Using the value of the cell:
below, above, on the right, on the left, and the same cell.

For 3 x 3 matrix it's:

b l l
b s a
r r a

For 4 x 4 matrix it's:

b l l l
b b l a
b r a a
r r r a

After a few minutes of trial and error, I've ended up with the following implementation:

  def newValueAt(in: Array[Array[Int]], i: Int, j: Int): Int = {
    val s = in.size - 1
    if      (i >= j && s - i >  j) in(i + 1)(j) // 'b'
    else if (i <  j && s - i >= j) in(i)(j - 1) // 'l'
    else if (i <= j && s - i <  j) in(i - 1)(j) // 'a'
    else if (i >  j && s - i <= j) in(i)(j + 1) // 'r'
    else                           in(i)(j)     // 's'
  }


Expose only desired functions to 3rd party plugins in JavaScript I tried to build a solution to dynamically load JS code, but to only expose predefined functions. Is this the correct approach? Is there any Method to execute arbitrary code if I put all my code inside of (function($$) { ... }).call({}, $$) statements?

// scope.js
// $$ holds all global variables, and is not accessible
// inside an plugin (unless there is a helper method in the API, which exposes the variable)

$$ = {};

    // (function($$) { ... }).call({}, $$) is used to set this to an empty object.
    // This prevents getting values from this, which should be in another scope
    // "use strict"; is used to prevent the creation of global variables

    // initialize the global variables
    (function($$) {
        "use strict";

        // read-only property
        $$.DEBUG = {
                get: function() {
                    return true;
                }
            };
            Object.defineProperty($$, 'DEBUG', $$.DEBUG);

        // execute the plugin code
        $$.exec = function (code) {
                return (function($$) {
                    /**/
                    // get the exposed API Methods
                    var API = new $$.API(),
                    $$ = {}; // Hide the global $$ object

                // prepare code for execution
                code = code.trim();
                if (code[code.length - 1] !== ';') {
                    code += ';';
                }
                var _code = "(function() { var document = {}, window = {}; " + code + " return this; }).call({})";

                // call with eval
                try {
                    // everything set with this will be returned after the eval
                    return eval(_code);
                } catch(e) {
                    console.error(e);
                }
                /**/
            }).call({}, $$);
            };
        }).call({}, $$);

    // $$.API() returns an object, which holds all exposed API methods
    // because $$.API() is an class, every plugin gets an local API oject.
    // If the elements of this object are overwritten, it does not interfere with other API objects
    (function($$) {
            $$.API = function() {
            // Default API values
            this.ApiVar = 3;

            // Expose global variables to the API
            this.setGlobalVar = function(value) {
                $$.globalVar = value;
                };
                this.getGlobalVar = function() {
                    return $$.globalVar;
                };
                return this;
            };
        }).call({}, $$);

    // setting a global variable
    "use strict";
    $$.globalVar = 15;

Usage ($$.globalVar inside the exec should return undefined):

// load the scopes
require('./scope.js');

"use strict";
(function($$) {
     /**/
     // restricted execution of code (only API is accessible)
     var ret = $$.exec("this.test = 'test' + ' API.ApiVar: ' + API.ApiVar + '\\n' + '$$.globalVar: ' + $$.globalVar + '\\n' + 'API.getGlobalVar(): ' + API.getGlobalVar(); API.setGlobalVar('test'); API.ApiVar = 16;");
     console.log(ret.test);
     console.log();
     // API Values are only changed locally
     console.log('API.ApiVar: ' + $$.API().ApiVar);
     // normal usage of $$ variables
     console.log('$$.globalVar: ' + $$.globalVar);
     /**/
    }).call({}, $$);

I used Node.js for testing but it shouldn't matter where it runs.

We are developing a desktop application, which uses HTML and JS as frontend. So it's almost a browser.

The wrapping with (function($$) { ... }).call({}, $$) is to get our code in its own local scope. that way, we can not expose function, which shouldn't be outside of the file they were used. ($$ is used to expose functions for our code)

The functions which are usable for 3rd party developers, are exposed through API(). So every "Plugin" (if you like to call it that) has its own local copy of API functions and cannot mess with global objects.

Interesting question,

first things first, it does matter where you run the code. In web browsers, this will always be Window, so eval.call( {} , 'console.log(this)' ) will log Window. link

And, of course, any code reviewer worth his salt will tell you that eval is dangerous, can you update your question and tell us why you need eval ?

In general, I would point you to this for Node for possibly safer ways of accomplishing what you need. For web browsers I would look into running JS in it's own frame ?


Transforming a byte array into an array of byte arrays with F# This code is intended to be used with an image manipulation library that I'm working on, and the goal is to take the raw byte[] data from an image, and transform it into a 2D array where each position on the array has a value corresponding to the pixel coordinates of the image.

Comments concerning how to do this faster or more aligned with F# standards, or concerning style and formatting, are very much appreciated. I'm still learning F# so I need all the help I can get.

let Transform2D (pArrayWidth:int) (pArray:byte[]) =
    let tHeight = pArray.Length / pArrayWidth
    let tImageArray = Array.zeroCreate<byte[]> tHeight

    let mutable tStart = 0
    let mutable tFinish = tStart + pArrayWidth - 1        // 0 indexed array

    for tRowIndex in [ 0 .. tHeight ] do
        tImageArray.[tRowIndex] <- pArray.[tStart .. tFinish]
        tStart <- tStart + pArrayWidth
        tFinish <- tFinish + pArrayWidth

    tImageArray

You should consider whether byte[][] is really the best type to represent a bitmap in F#.

On one hand, arrays are inherently mutable and don't work well with pattern matching, so you might want to consider using list<list<byte>>, especially if performance is not critical.

On the other hand, a rectangular array of bytes is more accurately modeled as byte[,] (2D array), not byte[][] (array of arrays, a.k.a. jagged array). Though again, this is going to be slower than the original option.

for tRowIndex in [ 0 .. tHeight ] do

This has to be tHeight - 1, otherwise your code will fail with an exception.

let mutable tStart = 0
let mutable tFinish = tStart + pArrayWidth - 1        // 0 indexed array

for tRowIndex in [ 0 .. tHeight ] do
    tImageArray.[tRowIndex] <- pArray.[tStart .. tFinish]
    tStart <- tStart + pArrayWidth
    tFinish <- tFinish + pArrayWidth

tImageArray

You can use sequence expressions to create arrays in a functional way. Here, that would look something like:

[|
    for tRowIndex in [ 0 .. tHeight - 1 ] do
        let tStart = tRowIndex * pArrayWidth
        let tFinish = tStart + pArrayWidth - 1
        yield pArray.[tStart .. tFinish]
|]

In both cases, a very similar syntax (for .. in) is used, but my version doesn't use any mutable variables and is very similar to list comprehensions in other functional languages.


XmlNodeList to ListItemCollection At this moment I'm reading an XmlNodeList into a ListItemCollection one node at a time using a foreach:

foreach (XmlNode node in authCompXml.SelectNodes("//Code"))
{
    CompaniesList.Items.Add(new ListItem(node.InnerText));
}

How can this be improved, such as without the aforementioned foreach?

CompaniesList.Items.AddRange(authCompXml.SelectNodes("//Code")
    .Cast<XmlNode>()
    .Select(node => new ListItem(node.InnerText))
    .ToArray());


Counting multiples of 3 up to a given number I'm attempting a practice problem in Codeforces that requires you to find the number of multiples of 3 less than or equal to a given number. I have written the following code but it gives a TLE once the test case reaches the order of 108.

int x = Reader.nextInt();
int count = 0;

for(int i = 3; i<=x; i++){
    if(i%3 == 0){
        count++;
    }
}
System.out.println(count);

Is there any way I can further optimize this code? The test cases max out at 109.

Use your brain, not brute force. How would you solve the problem if it were presented in a math test, and you couldn't use a computer?

Your solution requires \$O(x)\$ time; it should be possible in \$O(1)\$.


SQL to list function - turning a table column into a delimited list string I wrote this very "general use" function, because I needed that in some reporting. I was wondering if there is any suggetsion for improvement.

Function sql2List(sSql As String, Optional quote As String, Optional separ As String = ", ") As String
    Static db As DAO.Database
    Dim rs As DAO.Recordset
    Dim result As String
    'Debug.Print Now, sSql
    If db Is Nothing Then Set db = CurrentDb
    Set rs = db.OpenRecordset(sSql, dbOpenSnapshot)
    Do Until rs.EOF
        result = result & quote & rs.Fields(0).Value & quote & separ
        rs.MoveNext
    Loop
    rs.Close
    Set rs = Nothing
    'Set db = Nothing
    If Len(result) > Len(separ) Then sql2List = Left(result, Len(result) - Len(separ))
End Function

I call it from control or module or other queries like this:

strList = sql2List("select dt from planning where client = " & clientId)

In the context of the function

Function sql2List(sSql As String, Optional quote As String, Optional separ As String = ", ") As String
Static db As DAO.Database
Dim rs As DAO.Recordset
Dim result As String
'Debug.Print Now, sSql
If db Is Nothing Then Set db = CurrentDb
Set rs = db.OpenRecordset(sSql, dbOpenSnapshot)
Do Until rs.EOF
    result = result & quote & rs.Fields(0).Value & quote & separ
    rs.MoveNext
Loop
rs.Close
Set rs = Nothing
'Set db = Nothing
If Len(result) > Len(separ) Then sql2List = Left(result, Len(result) - Len(separ))
End Function

The static declaration of db is probably not a good idea.

This (my reformatting is just my preference) is probably better

Set db = CurrentDb
strList = sql2List(db, "select dt from planning where client = " & clientId)
Set db = Nothing

Function sql2List(db as DAO.Database,sSql As String, Optional quote As String, Optional separ As String = ", ") As String
Dim rs As DAO.Recordset
Dim result As String
Dim RetValue as string
  RetValue = ""
  'Debug.Print Now, sSql
  If not(db Is Nothing) = True Then
     Set rs = db.OpenRecordset(sSql, dbOpenSnapshot)
     Do Until rs.EOF
         result = result & quote & rs.Fields(0).Value & quote & separ
         rs.MoveNext
     Loop
     rs.Close
     Set rs = Nothing
     If Len(result) > Len(separ) Then
        RetValue = Left(result, Len(result) - Len(separ))
     endif
  ELSE
     RetValue =""
  endif
  sql2List= Trim(RetValue) ' I tend to want to remove leading and training spaces
End Function

There was nothing (much) wrong with what you did other that the static inside the function as that made the function less utilitarian because there was no way that function could ever be used for any connection other than the first one. It just so happens to work because you do not desire a different connection.


How can this function be faster? Solving for a row of Pascal's triangle I saw a posting on Hacker News this morning that was ranting about people not being able to solve an interview question. I thought I would give it a shot, but I would like to know how my attempt could be improved.

def get_pascal_row(n):
    """
    Returns the nth row of Pascal's Triangle for a given n. Uses
    Gray's algorithm.
    """
    if n == 0: return []
    n -= 1
    row = [1]
    for i in xrange(1,n+1):
        row.append(row[-1] * n/i)
        n -= 1
    return row

You can memoize the rows you have already computed previously.

Alternatively, you could compute it in closed form using the binomial coefficients:

Start looking in scipy.special.binom.


Find missing numbers in a range, with duplicate numbers in a sorted array Suggestions for cleanup and optimization request.

This question is a follow-up question on this post.

Lets assume the range of

  1, 2, 3, 4.

This function takes in an array of repeated elements, [1,1,4,4] and the output is [2, 3].

The question marked as duplicate takes an input of [1, 4] and output is [2, 3].
While output is same, the input differs.
Please make a note of this difference between two questions causing confusion.

final class Variables {
    private final int x;
    private final int y;

    Variables(int x2, int y2) {
        this.x = x2;
        this.y = y2;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    @Override public String toString()  {
        return "x = " + x + " y = " + y;
    }
}

    public final class FindMissingRepeating {

        /**
         * Takes an input a sorted array with only two variables that repeat once. in the
         * range. and returns two variables that are  missing.
         *
         * If any of the above condition does not hold true at input the results are unpredictable.
         *
         * Example of input:
         * range: 1, 2, 3, 4
         * input array [1, 1, 4, 4]
         * the variables should contain 2 missing elements ie 2 and 3
         *
         * @param a : the sorted array.
         */
        public static Variables sortedConsecutiveTwoRepeating (int[] a, int startOfRange) {
            if (a == null) throw new NullPointerException("a1 cannot be null. ");

            int low = startOfRange;
            int high = low + (a.length - 1);

            int x = 0;
            int i = 0;
            boolean foundX = false;

            while  (i < a.length) {
                if (a[i] < low) {
                    i++;
                } else {
                    if (a[i] > low) {
                        if (foundX) {
                            return new Variables(x, low);
                        } else {
                            x = low;
                            foundX = true;
                        }
                    }
                    low++;
                    i++;
                }
            }

            int val = foundX ? x : high - 1;
            return new Variables(val, high);
         }

        public static void main(String[] args) {
            int[] ar1 = {1, 2, 2, 3, 4, 4, 5};
            Variables v1 = FindMissingRepeating.sortedConsecutiveTwoRepeating (ar1, 1);
            System.out.println("Expected x = 6, y = 7 " + v1);

            int[] ar2 = {2, 2, 4, 4, 5, 6, 7};
            Variables v2 = FindMissingRepeating.sortedConsecutiveTwoRepeating (ar2, 1);
            System.out.println("Expected x = 1, y = 3 " + v2);

            int[] ar3 = {3, 3, 4, 4, 5, 6, 7};
            Variables v3 = FindMissingRepeating.sortedConsecutiveTwoRepeating (ar3, 1);
            System.out.println("Expected x = 1, y = 2 " + v3);

            int[] ar4 = {1, 3, 3, 4, 4, 6, 7};
            Variables v4 = FindMissingRepeating.sortedConsecutiveTwoRepeating (ar4, 1);
            System.out.println("Expected x = 2, y = 5 " + v4);
        }
    }

Firstly there are 2 classes so don't show them as a single code when showing your code. Place a heading with the class name then your code then second class name then your second class's code. That makes it easier to copy paste it into Eclipse when seeing the code.

Why have you named the parameters as x2 and y2 and still using the this keyword? Why are you making the variable names different in the first place? It just adds confusion. Name them as x and y and then use the this keyword. Period.

You are throwing a null pointer exception when a is null and the message is a1 is null. That will become confusing... Also you don't need to throw that explicitly. The assignment to high will throw the exception and give you a proper stacktrace already without any extra code.

The while loop can be made a for loop very easily and will limit the scope and extra variable (i in this case). Note that you are doing i++ in both if as well as else. That shows you didn't take the time to look at your own code.

You passed startOfRange to the function and forgot that it is a variable also. If you have any intention of not changing it then make it explicit and declare the parameter as final in the method's signature.

Also If I am correct then you have used x = 0 only to make it one less than the startRange so that your algorithm works. That means your function will break the moment you decide the startRange to be anything other than 1. Also using such magic numbers and such variable names as x is a bad idea. Really bad idea.

Take some time to refactor the code yourself. It pays if your code is easy to understand by other programmers. Your code can be changed to this and I think now it is much easier to understand the flow of execution of the current program.

public static ExVariables sortedConsecutiveTwoRepeating(int[] a,
        final int startOfRange) {

    int low = startOfRange;
    int high = low + (a.length - 1);

    int x = low - 1;
    boolean foundX = false;

    for (int i = 0; i < a.length; i++) {
        if (a[i] > low) {
            if (foundX) {
                return new ExVariables(x, low);
            }
            x = low;
            foundX = true;
        }
        if (a[i] >= low) {
            low++;
        }
    }

    if (foundX) {
        return new ExVariables(x, high);
    } else {
        return new ExVariables(high - 1, high);
    }
}

I think it should be possible to refactor it more so that the return comes only inside the for loop but I didn't take the time to do that.

Hope this helps.