texts:
Here is my code for "Reverse Binary Numbers" in C.

  Problem

  Your task will be to write a program for reversing numbers in binary.
  For instance, the binary representation of 13 is 1101, and reversing
  it gives 1011, which corresponds to number 11.

  Input

  The input contains a single line with an integer \$N\$, (where
  \$1≤N≤1000000000\$ ).

  Output Output one line with one integer, the number we get by reversing the binary representation of \$N\$.

  Sample 1

  input 13
  output 11

  Sample 2

  input 47
  output 61

It's pretty efficient, but not the best. I wanted to improve the runtime. So I am looking for suggestions to improve the code.

Code

//Reverse Binary

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

#define DIVISOR 2
#define MAX 50

int main() {
  int r, q, n, m = 1, num = 0, i = 0, len;
  int res[MAX];
  char str[MAX];

  scanf("%d", &n);

  do {

    q = n / DIVISOR;
    r = n % DIVISOR;

    res[i] = r;

    n = q;

    i++;

  } while(n > 0);

  len = i;
  m = 1;

  for(; i > 0; i--) {
    if(res[i - 1] != 0) {
      num += res[i - 1] * m;
    }
    m *= DIVISOR;
  }

  fprintf(stdout, "%d", num);

  return 0;
}


texts:
I have a validation which checks that a string is a valid asset number.  There are currently three formats it could be in, like: 001-123456, WSH002M52B, or CTR0025MLD.  The way my code is set up now is like:

public bool IsValidAssetNum(string assetNum)
{
    if (this.IsNorthAmericanAssetFormat(assetNum)) return true;
    if (this.IsGermanAssetFormat(assetNum)) return true;
    if (this.IsAustralianAssetFormat(assetNum)) return true;

    return false;
}

With each of those checks being like:

private bool IsNorthAmericanAssetFormat(string assetNum)
{
    var regex = new Regex(ComponentsResources.NorthAmericaAssetNumValidationRegex);
    var match = regex.Match(assetNum);

    return match.Success;
}

In the future, if more formats come up, we'll have to add a new specific format check as well as add it to the IsValidAssetNum function.  Is there a pattern out there to solve this issue?  I know a lot of if statement refactorings come from the command pattern or strategy pattern, but this seems like a different type of issue.


texts: Convert data back-and-forth between CSV files and pretty-print

texts:
I wrote a script to convert CSV files in human readable format and vice versa.

Human readable format is achieved like so:

$ ./transform table.csv inflate
cola colb      colc
1    a         g
2    b         g
3    c         g
4    d         g
5    eeeeeeeee g

And conversion back to CSV is done with:

$ ./transformtable table.t
cola;colb;colc;
1;a;g;
2;b;g;
3;c;g;
4;d;g;
5;eeeeeeeee;g;

Code:

import Data.List
import Data.List.Split
import System.Environment
main = do
  args <- getArgs
  file <- readFile (args !! 0)
  let lp = preprocess (lines file)
  let erg = (if elem "inflate" args then (inflate) else (compress)) lp
  mapM_ putStrLn $ map concat erg

rpad len string = take len $ string++(repeat ' ')

--ensure last col has elements
preprocess lines = map (rpad len) lines
             where len = maximum $ map (length) lines

--calc lengths of the chunks(colname + spaces) for splitplaces
dissect line = zipWith (\a b->length (a++b)) names spaces
          where names = wordsBy (==' ') line
                spaces= wordsBy (/=' ') (line++" ") --add " " to ensure space after last col-name

compress lines = add_del $ transpose $ map (shrinkcol) cols
            where delim       = ';'
                  head_lens   = dissect (head lines)
                  cols        = transpose $ map (splitPlaces head_lens) lines
                  shrinkcol c = map (\el -> reverse $ dropWhile (==' ') $ reverse $ dropWhile (==' ') el) c
                  add_del     = map (map (++[delim]))

inflate lines = transpose $ map padcol $ table
          where table = transpose $ map (splitOn ";") lines --list of cols
                padcol c = map (rpad m) c --pad whole col
                   where m = (1+) $ maximum $ (map length) c --determine longest string in col, pad one more for prettiness

I'm relatively new to haskell, so I'm wondering if this can be done more elegantly.


texts: Verifying if two numbers are equal using ArrayLists

texts:
I have some question about the code below. It works correctly, but:

Is there a better/another way to solve the exercise (as an expert would have done:) or mine it's perfectly done?
Also, I don't see the point of using ArrayList instead of a normal vector like int a[].
In the method analysis I wanted to set as parameters (ArrayList a,ArrayList b) but an error (incompatible types List cannot be converted to ArrayList) appears in this int a = Testing.analysis(seq1, seq2, t1); line. I thought  I was working with ArrayList since I instantiated my sequences as such.

Exercise:

Perform a program that uses a function that returns a 1 if two very large numbers (digits greater than 5 and where each digit is an element of the arrayList) are equal and a 0 otherwise.
    Example 1:
    Entry
    Number 1: 2 3 4 5 6 7
    Number 2: 1 4 6 8 9 0
    Output "They are not equal numbers"

class Testing {

static int analysis(List a, List b, int t) throws Exception { //using
//ArrayList instead of List marks an error, why?

    int c = 0;

    if (a.size() == b.size() && a.size() > 5) {

        for (int i = 0; i < t; i++) {
            if (a.get(i) == b.get(i)) {
                c++;
            }
        }
    } else {
        System.out.println("oops! the lengths are not the same or some
    of the sequence is less than 6");
        throw new Exception(); //to finish the method
    }

    if (c == t) {
        return 1;
    } else {
        return 0;
    }

}
}

public class Tarea24agoENGLISH {

public static void main(String[] args) throws Exception {
    Scanner q = new Scanner(System.in);

    List seq1 = new ArrayList(); //I wanted to write this List <int> seq=new ArrayList<int>(); but it marked an error, why?
    System.out.println("Introduce the length of the sequence1 ");
    int t1 = q.nextInt();

    for (int i = 0; i < t1; i++) {
        System.out.println("Introduce the digits for sequence1");
        int n = q.nextInt();
        seq1.add(n);
    }

    List seq2 = new ArrayList();
    System.out.println("Introduce the length of the sequence2");
    int t2 = q.nextInt();

    for (int i = 0; i < t2; i++) {
        System.out.println("Introdce the digits for sequence2");
        int n = q.nextInt();
        seq2.add(n);
    }

    int a = Testing.analysis(seq1, seq2, t1);
    if (a == 1) {
        System.out.println("same numbers");
    } else {
        System.out.println("\n different numbers");
    }

}

}


texts: Idiomatic way to construct a SQL INSERT query in Python

texts:
I have a bit of code to insert some values in a table. It seems a bit clunky - is there a neater way?

words is a list of dictionaries, and the entries contain other fields which I don't want to insert.

# add the list of words to the words table
time_stamp = datetime.datetime.now().isoformat()
sql = "INSERT INTO words (word, user_id, game_id, score, time_stamp) VALUES "
vals = []
for word in words:
    vals.append("('%s', %s, %s, %s, '%s')" %
                (word['str'], user_id, game_id, word['score'], time_stamp))
cur.execute(sql + ",".join(vals))


texts: How can I make my filter search code more efficient?

texts:
Here is App.js

const listItems=document.querySelector(".student-list").children;
const studentList=document.querySelector('.student-list');
const numberOfItems=10;
const page=document.querySelector(".page");

displayPage=(list,pageNumber)=> {
   const SI=(pageNumber*numberOfItems)-numberOfItems;
   const EI=pageNumber*numberOfItems;
   Array.from(list).forEach((item,index)=> {
      if (index>= SI && index<EI) {
         item.style.display="block";
      } else {
         item.style.display="none";
      }
   })
}

addPaginationLinks=(list)=> {
   const pages=Math.floor(list.length/10)
   let html=``;
   for (let i=0; i<pages; i++) {
      if (i===0) {
         html+=`

            ${i+1}

`;
      } else {
         html+=`

            ${i+1}

`;
      }

   }
   const ul=document.createElement("ul");
   ul.innerHTML=html;
   const div=document.createElement("div");
   div.classList.add("pagination");
   div.appendChild(ul);
   page.appendChild(div);
}

displayPage(listItems,1);
addPaginationLinks(listItems);

addEventListener=()=> {
   const a=document.querySelectorAll("a");
   a.forEach(item=> {
      item.addEventListener('click', (e)=> {
         a.forEach(item=> {
            if (item.classList.contains("active")) {
               item.classList.remove("active")
               e.target.classList.add("active");
            }
         })
         const pageNumber=parseInt(e.target.textContent);
         displayPage(listItems,pageNumber);
      })
   })
}

addEventListener();

addSearchComponent=()=> {
   const pageHeader=document.querySelector(".page-header.cf")
   let html=`

      <input placeholder="Search for students...">
      <button>Search</button>

`;
   pageHeader.insertAdjacentHTML('beforeend', html);
}

addSearchComponent()

const search=document.querySelector("input");
const studentDetails=document.getElementsByClassName("student-details");

noResultsItem=()=> {
   const item=`

No Results Shown Found

`;
   studentList.insertAdjacentHTML('beforeend', item);
}

noResultsItem()

search.addEventListener('keyup', (e)=> {
   const noResults=document.querySelector(".no-results");
   const array=Array.from(studentDetails).filter(student=>student.children[1].textContent.includes(e.target.value))
   if (array.length==0) {
      Array.from(studentDetails).forEach(student=>{
         student.parentNode.style.display="none";
      });
      noResults.style.display="block"
   } else if (array.length>0) {
      noResults.style.display="none"
      Array.from(studentDetails).forEach(student=>{
         if (student.children[1].textContent.includes(e.target.value)) {
            student.parentNode.style.display="block";
         } else {
            student.parentNode.style.display="none";
         }
      });
   }
});

I'm trying to refactor the Javascript code at the bottom of my app.js.

There's a keyup event in the search input, and when no results are found, I want to display the no results li item and also make it disappear immediately in case a user types in a character that creates a search match. However, I feel like I could have written this much better. I made the li's display none by default, but it feels tedious to re-alter it to display: none and display: block in both the if conditions.

Does anyone have any suggestions to a beginner programmer on how to make the code more efficient?


texts: Showing a table for open and closed tickets

texts:
This shows a table for open and closed tickets. The code for open tickets is identical with the closed tickets. I was trying to figure out a way to just write one code segment that will show both tables and use half of the code lines. With some changes I ended up to this code. With this code, if I iterate it twice and at at first use the word "open" and then rename it to "closed" then I will accomplish what I am after.

I looked around and figured out that I need to use a dynamic variable name. Something like sum#{status}tickets (pretty sure that something like that doesn't work) where the "status" variable will be at the first iteration "open" and then "closed" at the second one.

I was wondering if this is somehow possible (to iterate twice) and then dynamically set the variable's name.

   <% if sumopentickets == 0 %>

Open Tickets

No open tickets.

  <% else %>

Open Tickets

      Ticket Number
      Title
      Submitted by
      Created at
      Status

    <% @tickets.each do |ticket| %>
    <% if ticket.status == "open" %>
    <% user = User.find(ticket.user_id) %>

        <%= ticket.id %>
        <%= link_to ticket.title, ticket_path(ticket.url_token) %>
        <%= user.username %>
        <%= ticket.created_at.strftime("%H:%M, %B %d, %Y") %>
        <% if ticket.ticketmessages.last && ticket.ticketmessages.last.user_id %>
        (user waiting for reply...)
        <% end %>

    <% end %>
    <% end %>

  <% end %>

  <% if sumclosedtickets == 0 %>

Closed Tickets

No closed tickets.

  <% else %>

Closed Tickets

      Ticket Number
      Title
      Submitted by
      Created at
      Status

    <% @tickets.each do |ticket| %>
    <% if ticket.status == "closed" %>
      <% user = User.find(ticket.user_id) %>

        <%= ticket.id %>
        <%= link_to ticket.title, ticket_path(ticket.url_token) %>
        <%= user.username %>
        <%= ticket.created_at.strftime("%H:%M, %B %d, %Y") %>
        <% if ticket.ticketmessages.last && ticket.ticketmessages.last.user_id %>
        (user waiting for reply...)
        <% end %>

    <% end %>
    <% end %>

  <% end %>


texts: *nix, *bsd, etc basic `tput` color setup

texts:
My goal with the below piece of POSIX shell code was to address the more platforms the better with shell tput colors. With this code, I now start all of my scripts, and so it's time to review it for some things I overlooked, did not think of too well, and such. Note: I start my scripts with set -u, which is why I set all empty in unspecified cases. Thanks.

#!/bin/sh

set -u

if tput setaf > /dev/null 2>&1; then
    # Linux-like
    tput_number_of_colors=$(tput colors 2> /dev/null)
    tput_bold=$(tput bold 2> /dev/null)
    tput_reset=$(tput sgr0 2> /dev/null)
    tput_cmd_set_fg_color='tput setaf'
elif tput AF > /dev/null 2>&1; then
    # BSD-like
    tput_number_of_colors=$(tput Co 2> /dev/null)
    tput_bold=$(tput md 2> /dev/null)
    tput_reset=$(tput me 2> /dev/null)
    tput_cmd_set_fg_color='tput AF'
else
    # Console-like
    tput_number_of_colors=2
    tput_cmd_set_fg_color=
    tput_bold=
    tput_reset=
fi

tput_test ()
{
    [ -n "$tput_number_of_colors" ] && [ -n "$tput_bold" ] && [ -n "$tput_reset" ] &&
    { [ "$tput_number_of_colors" -ge 8 ] && printf '%s' "$tput_bold" && $tput_cmd_set_fg_color 1; } > /dev/null 2>&1
}

if tput_test; then
    color_red=$tput_bold$($tput_cmd_set_fg_color 1)
    color_green=$tput_bold$($tput_cmd_set_fg_color 2)
    color_yellow=$tput_bold$($tput_cmd_set_fg_color 3)
    color_blue=$tput_bold$($tput_cmd_set_fg_color 4)
    color_magenta=$tput_bold$($tput_cmd_set_fg_color 5)
    color_cyan=$tput_bold$($tput_cmd_set_fg_color 6)
    color_white=$tput_bold$($tput_cmd_set_fg_color 7)
else
    color_red=; color_green=; color_yellow=; color_blue=; color_magenta=; color_cyan=; color_white=
fi


texts: Test if a number is within 2 of a multiple of 10

texts:
I have solved this exercise, but it looks really redundant. Is there a built-in function in Java or a cleaner way to caluclate mod of a value + deviation without multiple OR statements? If I had to calculate this for a deviation of 5, this would then require a lot of repetition.

  Given a non-negative number "num", return true if num is within 2 of a multiple of 10. Note: (a % b) is the remainder of dividing a by b, so (7 % 5) is 2.

    public boolean nearTen(int num) {
        return isMod10(num) || isMod10(num - 1) || isMod10(num + 1) || isMod10(num - 2) || isMod10(num + 2);
    }

    private boolean isMod10(int num) {
        return num % 10 == 0;
    }


texts:
I'm currently learning how to use Apache Spark. In order to do so, I implemented a simple wordcount (not really original, I know). There already exists an example on the documentation providing the needed code. However, I tried to go one step further and to produce the result as a sorted list (by frequence) by using the spark modules.

A use case would be sortedWordcount("I fish a fish") ->  [(fish, 2), (I,1), (a,1))]

Here is how I proceed:

public List<String> sortedWordcount() {
    SparkConf conf = new SparkConf().setAppName("wordcount").setMaster("local");
    JavaSparkContext sc = new JavaSparkContext(conf);

    JavaRDD<String> lines = sc.textFile("myFile.txt");

    // classical wordcount, nothing new there
    JavaRDD<String> words = lines.flatMap(new FlatMapFunction<String, String>() {
                public Iterable<String> call(String s) {
                    return Arrays.asList(s.split(" "));
                    }
                });
    JavaPairRDD<String, Integer> pairs = words.mapToPair(new PairFunction<String, String, Integer>() {
                public Tuple2<String, Integer> call(String s) {
                    return new Tuple2<String, Integer>(s, 1);
                }
            });
        JavaPairRDD<String, Integer> counts = pairs.reduceByKey(new Function2<Integer, Integer, Integer>() {
                public Integer call(Integer a, Integer b) {
                    return a + b;
                }
            });

    // to enable sorting by value (count) and not key -> value-to-key conversion pattern
    JavaPairRDD<Tuple2<Integer, String>, Integer> countInKey = counts.mapToPair(a -> new Tuple2(new Tuple2<Integer, String>(a._2, a._1), null)); // setting value to null, since it won't be used anymore

    List<Tuple2<Tuple2<Integer, String>, Integer>> wordSortedByCount = countInKey.sortByKey(new TupleComparator(), false).collect();

    List<String> result = new ArrayList<>();
    IntStream.range(0, wantedSize).forEach(i -> result.add(wordSortedByCount.get(i)._1._2));
    return result;
}

private class TupleComparator implements Comparator<Tuple2<Integer, String>>, Serializable {
    @Override
    public int compare(Tuple2<Integer, String> tuple1, Tuple2<Integer, String> tuple2) {
        return tuple1._1 < tuple2._1 ? 0 : 1;
    }
}

To sum up on this code work:

it stores the text as a list of word
it changes this list of word into a list of tuple, each tuple being (word, 1) with word as key
it combine tuples with the same key and counts them, tuples are now (word, freqWord)
it changes the way tuples are displayed, they now become ((freqWord, word), null)
tuples are sorted by key (see the Comparator implementation)

My main concern is about performances, but I'll gladly welcome any critiques on any aspects of this code. Is there any way to achieve the same goal in less / more efficient steps?


texts: Using components instead controllers in EmberJS v2

texts:
I am beginner in EmberJS. I have made todomvc for ember v2 for learning Ember.

My implementation works without controller. I use components instead controllers.

In the documentation I see:

  Controllers are very much like components, so much so that in future versions of Ember, controllers will be replaced entirely with components.

But I think it is some ugly solution. Now I will explain why.

This is template for the todo item component for my todolist
app/templates/components/todo-item.hbs:

    {{#if isEditing}}
        {{input-focused class="edit" value=item.title focus-out="acceptChanges" insert-newline="acceptChanges"}}
    {{else}}
      {{input type="checkbox" checked=isCompleted class="toggle"}}
      {{item.title}}<button {{action "removeTodo"}} class="destroy"></button>
    {{/if}}

It has acceptChanges and removeTodo events. And in the JS part of component I have handlers:

//app/components/todo-item.js
import Ember from 'ember';

export default Ember.Component.extend({

    updateTodoAction: 'updateTodo',
    deleteTodoAction: 'deleteTodo',

    isEditing: false,

    // some lines removed for brevity

    actions: {
      editTodo: function() {
        this.set('isEditing', true);
      },

      acceptChanges: function() {
        this.set('isEditing', false);
        this.sendAction('updateTodoAction', this.get('item'), this.get('item.title'));
      },

      removeTodo: function () {
        this.sendAction('deleteTodoAction', this.get('item'));
      }
    }
});

In the JS code of component I don't work with storage. I send this.sendAction(), and my route will handle it. And todos-route works with storage.

Please look at todos route:

// app/routes/todos.js
import Ember from 'ember';

export default Ember.Route.extend({
    model() {
        return this.store.findAll('todo');
    },

    actions: {
        // some lines removed from here for brevity

        updateTodo: function(todo, title) {
            if (Ember.isEmpty(title)) {
                this.send('deleteTodo', todo);
            } else {
                todo.save();
            }
        },

        deleteTodo: function(todo) {
            todo.deleteRecord();
            todo.save();
        },

       // some lines removed from here for brevity
    }
});

When the user clicks on the remove button, it generates removeTodo action and it will handled in removeTodo() in the JS part of the component. And then the removeTodo() function calls  this.sendAction('deleteTodoAction' ... ), which generates deleteTodo action. This action will handled in todos route in  deleteTodo() method. I have todo.save() in this method.

The acceptChanges action works much like removeTodo, but it sets the property isEditing = false before calling this.sendAction.

I have following chain for actions:

component template -> component js -> route

I think for deleteTodo(), it will be better to exclude the component JS part. But how?

In updateTodo() I need the component JS part, because I set the value for the isEditing property. But for calling route, I need the updateTodoAction property. And it is ugly for me. Can it work without the updateTodoAction property? How?

Also, I'd like to see any your comments about my solution (components instead controllers) or any part of my code.


texts: Identifying spreadsheet clashes in Google Apps script

texts:
This is an attempt at porting some of my boss's code from VBA to Google Apps. Are there more elegant ways of doing this? I know that later versions of ECMAScript can do this more nicely but Google Apps Script seems to be missing some features from that.

function checkDoubleBooking() {
  var sData = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Schedule").getDataRange().getValues();
  for (var row=scheduleStartRow-1; row<sData.length; row++) {
    var teacher = sData[row][scheduleTeacherCol-1];
    var room = sData[row][roomNoCol-1];
    if (!cellEmpty(teacher) || !cellEmpty(room)) {
      var semesterNo = sData[row][semesterCol-1];
      for (var clashRow=row+1; clashRow<sData.length; clashRow++) {
        var clashTeacher = sData[clashRow][scheduleTeacherCol-1];
        var clashRoom = sData[clashRow][roomNoCol-1];
        var clashSemester = sData[clashRow][semesterCol-1];
        var teacherClashPossible = (teacher == clashTeacher) && (!cellEmpty(teacher));
        var roomClashPossible = (room == clashRoom) && (!cellEmpty(room));
        var anyClashPossible = (teacherClashPossible || roomClashPossible);
        if ((anyClashPossible) && (semesterNo == clashSemester)) {
          originalSlots = sData[row][slotsCol-1].replace(" ","").split(",");
          clashSlots = sData[clashRow][slotsCol-1].replace(" ","").split(",");
          var clashPossible = false
          for (var originalSlot=0; originalSlot<originalSlots.length; originalSlot++) {
            if(clashSlots.indexOf(originalSlots[originalSlot]) != -1) {
              clashPossible = true
              break
            }
          }
          if (clashPossible) {
            for (var week=1; week<=12; week++) {
              if (!cellEmpty(sData[row][weekColsInArray(week)])) {
                if (!cellEmpty(sData[clashRow][weekColsInArray(week)])) {
                  var colour;
                  if (roomClashPossible && teacherClashPossible) {
                    colour = staffAndRoomClashColour;
                  } else {
                    if (roomClashPossible) {
                      colour = roomClashColour;
                    } else {
                      colour = staffClashColour;
                    }
                  }
                  fillClashColour(row+1, weekColsInArray(week)+1, colour);
                  fillClashColour(clashRow+1, weekColsInArray(week)+1, colour);
                }
              }
            }
          }
        }
      }
    }
  }
}


texts:
I'm trying to see if there is a better way to do this when I just need one column with one row. Do not mind the lack of use of a prepared statement, I will put it in.

public <T> T getValueFromDb(String col, String table, String cond, Class<T> returnValueClass) {

    T val = null;

    try {
        ResultSet rs = getSet("SELECT " + col + " FROM " + table + " WHERE " + cond);
        if(rs.next()) {
            if(returnValueClass == Integer.class) {
                val = (T) (Integer) rs.getInt(col);
            } else if(returnValueClass == Double.class) {
                val = (T) (Double) rs.getDouble(col);
            } else if(returnValueClass == String.class) {
                val = (T) rs.getString(col);
            }
        }
        closeSetQuietly(rs);
    } catch(SQLException sql) {
        Log.logException(null, sql);
    }

    return val;

}

Should I just perhaps do just:

returnValueClass.cast(rs.getInt(x));


texts:
I translated Peter Norvig Python Sudoku solver to Scala and my Scala implementation was slower in the order of few magnitudes e.g. instantaneous, in Python, vs 4 to 5 seconds. I suspected I didn't implement the algorithm to the dot or the way I use the Scala classes less than ideal. These are the crucial functions I translated from the Python functions using the same names. Thanks.

def eliminate(solution: Map[String, String], cell: String, num: Char):
Option[Map[String, String]] = {

val nums = solution(cell)

if (nums.contains(num)) {
  val updatedSoln = solution.updated(cell, nums.replace(num.toString, ""))
  val ys = updatedSoln(cell)

  val emSoln = if (ys.isEmpty) None
  else if (ys.size == 1)
    peers(cell).foldLeft(Option(updatedSoln))((s, c) =>
      s.flatMap(s2 => eliminate(s2, c, ys.head)))
  else Some(updatedSoln)

  emSoln.flatMap(s => {
    val finalSoln = peersAndSelf(cell).filter(c => s(c).contains(num))
    if (finalSoln.isEmpty) None
    else if (finalSoln.size == 1) assign(s, finalSoln.head, num)
    else emSoln
  })
 } else Some(solution)
}
def assign(solution: Map[String, String], cell: String, num: Char): Option[Map[String, String]] = {
if (debug) println(s"Assign ($cell, $num)")
solution(cell).replace(num.toString, "").foldLeft(Option(solution))((s, c) => s.flatMap(m => eliminate(m, cell, c)))
}

def search(solution: Map[String, String]): Option[Map[String, String]] =
solution.filter(_._2.size > 1).toList match {
  case Nil => Some(solution)
  case xs =>
    val (cell, nums) = xs.minBy(_._2.size)
    nums.flatMap(d => assign(solution, cell, d) match {
      case Some(x) => search(x)
      case None => None
    }).headOption
}

I am not comparing which language is faster. I happy if Python takes 300ms and the refactor Scala code takes 800ms.

Click here for the full implementation.


texts: Given 2 disjoint sets of intervals, find the intersections

texts:
def getIntersection(set_item1, set_item2):
    (l11, l12) = set_item1
    (l21, l22) = set_item2
    if (l11 > l21 and l11 < l22) or (l12 > l21 and l12 < l22) or (l21 > l11 and l21 < l12) or (l22 > l11 and l22 < l12):
        if l11 > l21:
            start = l11
        else: start = l21

        if l12 > l22:
            end = l22
        else:
            end = l12
        return (start ,end)
    return None

def returnInersection(set1, set2):
    for l1 in set1:
        for l2 in set2:
            ret = getIntersection(l1, l2)
            if ret:
                print ret

I think above one works fine but it runs in O(n2). Is there any better solution?

Example input can be set1=[(1, 6), (10, 15), (20, 30)] and set2=[(2, 5), (7, 13)].


texts: Review the vimrc for a Python programmer

texts:
I am primarily a Python programmer and have finally ditched the IDE in favour of vim and I will admit, I am loving it !

My vimrc file looks like this:

autocmd BufRead,BufNewFile *.py syntax on
autocmd BufRead,BufNewFile *.py set ai
autocmd BufRead *.py set smartindent cinwords=if,elif,else,for,while,with,try,except,finally,def,class
set tabstop=4
set expandtab
set shiftwidth=4
filetype indent on

Any changes I should make to make my Python vim experience more pleasant?


texts: Foo - Control | Naming conventions update

texts:
The code below is for modern browsers only.

Naming conventions are as follows:

m or M - Model

v or V - View

c or C - Controller

   /********************************************************************************************
     ********************************************************************************************
     *( C )ontrol
     *  Cin -  Input Path
     *  COut - Ouput Path
     ********************************************************************************************
     *******************************************************************************************/

    /**
     *COut
     */

    var COut = ( function ()
    {
        var messages =
        {
            name:         'Please enter a valid name',
            email:        'Please enter a valid email',
            email_s:      'Please enter a valid email.',
            pass:         'Please enter password, 6-40 characters',
            url:          'Please enter a valid url',
            title:        'Please enter a valid title',
            tweet:        'Please enter a valid tweet',
            empty:        'Please complete all fields',
            same:         'Please make emails equal',
            taken:        'Sorry, that email is taken',
            validate:     'Please contact support to reset your password'

.

..

...

....


texts:
I have this interpretation of John Conway's Game of Life in Python 3.3.1:

#JOHN CONWAY'S GAME OF LIFE
def countSurrounding(universe, a, b):
    count = 0
    surrounding = [[a - 1, b - 1],
                   [a - 1, b    ],
                   [a - 1, b + 1],
                   [a    , b - 1],
                   [a    , b + 1],
                   [a + 1, b - 1],
                   [a + 1, b    ],
                   [a + 1, b + 1]]
    for a in range(0, 8):
        try:
            if universe[surrounding[a][0]][surrounding[a][1]] == 1:
                count += 1
        except IndexError: a
    return count
def printUniverse(universe):
    for a in universe:
        print(a)
    print("\n")
nextUniverse = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 1, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 1, 0, 0, 0, 0],
                [0, 0, 1, 1, 1, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0]]
for gen in range(0, 10):
    universe = [a[:] for a in nextUniverse]
    printUniverse(universe)
    for a in range(0, 9):
        for b in range(0, 9):
            if universe[a][b] == 0 and countSurrounding(universe, a, b) == 3:
                nextUniverse[a][b] = 1
            elif universe[a][b] == 1 and countSurrounding(universe, a, b) not in (2, 3):
                nextUniverse[a][b] = 0

How can I improve/optimize my code?

EDIT: After the answers below, I have updated my code:

#JOHN CONWAY'S GAME OF LIFE
def countSurrounding(universe, a, b):
    count = 0
    surrounding = ((a - 1, b - 1),
                   (a - 1, b    ),
                   (a - 1, b + 1),
                   (a    , b - 1),
                   (a    , b + 1),
                   (a + 1, b - 1),
                   (a + 1, b    ),
                   (a + 1, b + 1))
    for a, b in surrounding:
        if not(a < 0 or b < 0 or a >= len(universe) or b >= len(universe[a])) and universe[a][b]:
            count += 1
    return count
def printUniverse(universe):
    for a in universe:
        print(a)
    print("\n")
nextUniverse = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 1, 0, 0, 0, 0, 0],
                [0, 0, 0, 1, 0, 0, 0, 0, 0],
                [0, 0, 0, 1, 0, 0, 0, 0, 0],
                [0, 0, 0, 1, 0, 0, 0, 0, 0],
                [0, 0, 0, 1, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0]]
for gen in range(0, 10):
    universe = [a[:] for a in nextUniverse]
    printUniverse(universe)
    for a in range(0, len(universe)):
        for b in range(0, len(universe[a])):
            if universe[a][b] == 0 and countSurrounding(universe, a, b) == 3:
                nextUniverse[a][b] = 1
            elif universe[a][b] == 1 and countSurrounding(universe, a, b) not in (2, 3):
                nextUniverse[a][b] = 0

Any more improvements to be made?
So far I have:

Introduced a check in countSurrounding to replace the except IndexError, so that the board does not wrap.
Made surrounding a tuple instead of a list
Changed for a in range(0, 9) to for a in range(0, len(universe)), as I assume that is more pythonic


texts: Compute and Display Engineering Information

texts:
I am updating a class in legacy code and having some issue with my calcPressure method.   I want to make it nice and clean but there is some convolution in the process.

Namely the calcPressure method I have does this:

computes a value from existing values
writes that value into an in-class parameter
returns the value to the caller
prints debug output while computing
prepares output for View (JSON, document...innerHtml)

Issues

In general, the code is all interconnected and it works, so part of me feels "why do I want to mess with it".  But part of me feels that Separation of Concerns (SOC) is super-broken.  After all, the same internal pressure parameter that originates inside PHP class goes all the way into JS view, from being computed in PHP, transferred to JS and displayed in the browser.  So if I make pressure private for example, or rename it, my view will become broken, since it depends on having the same parameter name in JS as in PHP for it to work.

More specifically my method seems to do an awful lot.  I'm questioning whether I should only have it compute data and return it without assigning the in-class parameter.  Or if I should only assign the in-class parameter and not return anything.  Right now it does both.  I wonder if I should redirect debugging information to be a part of view and not be a part of the method somehow (computation/debug output separation).

Code

class Spec
{

    function load()
    {
         $result = db_query("SELECT * from spec where id = {$this->id}");
         $row = db_fetch_array($result);
         $this->n = $row['n'];
         $this->sg = $row['sg'];
         $this->q = $row['q'];
    }

    /**
     * Computes and returns Pressure
     * Outputs debug info
     * Assigns internal parameter
     *
     * @return number
     */
    function calcPressure()
    {
        $res = abs($this->n * sqrt($this->q) / 15164.93 * $this->sg);
        dump("  *** Pressure = $res");
        $this->pressure = $res;
        return $res;
    }

    public $pressure;

    public $sg;

    public $q;

    public $n;

    public $id;
}

PHP side

$spec = new Spec();

/*
 * Load of static parameters ($q, $sg, $n) from DB omitted for clarity
 */
$spec->id = 5;
$spec->load();
$spec->calcPressure();
$json = json_encode($spec);

JS/View side

Dump

function dump($sql){print "
";print_r($sql);print"
" . PHP_EOL;}


texts:
I am trying to learn how to animate with tkinter. The code below is an example I was able to build. It creates a small 5x5 map of cubes, then one of them randomly moves around the screen (preferably on the map, but it can wander off).

Is this a good way to do it?  Is there a more pythonic, or more efficient way of doing it?

from tkinter import *
from time import sleep
from random import randrange

class alien(object):
    def __init__(self):
        self.root = Tk()
        self.canvas = Canvas(self.root, width=400, height=400)
        self.canvas.pack()
        self.map = [[1, 0, 0, 1, 0], [0, 1, 0, 1, 0], [0, 0, 1, 0, 0], [0, 1, 1, 0, 0], [1, 0, 0, 1, 0]]
        self.x = 0
        self.y = 0
        for row in range(len(self.map)):
            for column in range(len(self.map[row])):
                color = "green"
                if self.map[row][column] == 1:
                    color = "black"
                #print(50 * row + 50, 50 * column + 50)
                self.canvas.create_rectangle(50 * row , 50 * column , 50 * row + 50, 50 * column + 50,
                    outline=color, fill=color)
        self.creature = self.canvas.create_rectangle(50 * self.x, 50 * self.y, 50 * self.x + 50, 50 * self.y + 50,
            outline="red", fill="red")
        self.canvas.pack(fill=BOTH, expand=1)
        self.root.after(0, self.animation)
        self.root.mainloop()

    def animation(self):
        while True:
            i = randrange(1, 5)
            print(i)
            if i == 1:
                y = -1
            elif i == 2:
                y = 1
            elif i == 3:
                x = -1
            elif i == 4:
                x = 1
            sleep(0.025)
            print(self.x, self.y)
            self.canvas.move(self.creature, self.x, self.y)
            self.canvas.update()
alien()


texts:
Find 3 elements that sum to zero in an int array..  I would like to be reviewed on efficiency, style, and obviously if there is a bug I would like to know.

def sum_to_zero(a):
  ''' given an array of ints, find 3 elements that sum to 0 '''
  for i in a:
    for j in a[1:]:
      for k in a[2:]:
        if i + j + k == 0:
          return (i,j,k)


texts: Optimizing Java SHA-512 String-to-Hash Generator

texts:
In an attempt to learn Java and SHA-2 I created a very simple String-to-SHA512 generator. Here is the code:

package main;
import java.util.Scanner;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SHA512_String_Hash {
    public static void main(String args[]) throws NoSuchAlgorithmException {
        Scanner inputScanner = new Scanner(System.in);
        System.out.print("Text Input: ");
        String input = inputScanner.next();

        MessageDigest objSHA = MessageDigest.getInstance("SHA-512");
        byte[] bytSHA = objSHA.digest(input.getBytes());
        BigInteger intNumber = new BigInteger(1, bytSHA);
        String strHashCode = intNumber.toString(16);

        while (strHashCode.length() < 128) {
            strHashCode = "0" + strHashCode;
        }
        System.out.println("SHA-512: \n" + strHashCode);
    }
}

Since I am new to SHA-512 in Java, I do not know the optimal (for performance and more importantly security) way to create a SHA-512 hash generator.

TLDR— How could the performance and security of this code be improved?


texts:
This will test if a user entered an pangram (every letter in alphabet used at least once), however with 4 for loops there's got to be a better algorithmic approach or with the language of C itself.

pangram.c

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

 typedef enum { true, false } bool;

 typedef struct node{
     char letter;
     bool exists;
 } node;

 int main(int argc, char *argv[]){
     int SIZE = 500;
     char input[SIZE];

     printf("Enter your pangram: ");
     fgets(input, SIZE, stdin);

     // 26 letters
     node alphabet[26];
     int i = 0;
     for(char c='a'; c<='z'; ++c, i++){
         alphabet[i].letter = c;
         alphabet[i].exists = false;
     }

     for(int i=0; i<SIZE; i++){
         for(int j=0; j<27; j++){
             if(isalpha(input[i]) && input[i] == alphabet[j].letter){
                 alphabet[j].exists = true;
             }
         }
     }

     for(int i=0; i<27; i++){
         if(alphabet[i].exists==false){
             printf(" no pangram, missing letter.\n");
             return 1;
         }
     }

     printf("you've entered a pangram.\n");

     return 0;
 }

command line:

>> gcc -o pangram pangram.c -std=c99; ./pangram
Enter your pangram: this should fail
 no pangram, missing letter.

>> gcc -o pangram pangram.c -std=c99; ./pangram
Enter your pangram: the quick brown fox jumps over the lazy dog
you've entered a pangram.


texts: Is this the correct way to implement a HMAC token system?

texts:
I'm architecting a basic SSO solution to pass a user between two sites.

Site A handles authentication against a user database and then generates links to Site B which pass a HMAC token comprising a nonce and the user's ID. The HMAC is hashed with a shared secret between the two sites.

The following shows the token generation in PHP.

$sso['nonce'] = hash('sha1', rand() );
$sso['sharedKey'] = 'someVerySecureString';
$sso['hmacKey'] = hash_hmac ( 'sha256', $test['sharedKey'] , $test['nonce'] );
$sso['userId'] = '123456';
$sso['hmac'] = hash_hmac ( 'sha256', $test['userId'] , $test['hmacKey'] );
$sso['url'] = '/?id=' . urlencode($test['userId']) . '&nonce=' . urlencode($test['nonce']) . '&hmac=' . urlencode($test['hmac']);

I'm using the nonce to HMAC the key before transmission.

This is the PHP function on Site B to validate the token:

function validateSSOToken($sharedKey='someVerySecureString') {

    $isValid = false;

    if ( isset( $_GET['id'] )
      && isset( $_GET['nonce'] )
      && isset( $_GET['hmac'] )
      && strlen( $_GET['nonce'] ) == 40
      && strlen( $_GET['hmac'] ) == 64) {

        $userId = $_GET['id'];
        $nonce = $_GET['nonce'];
        $hmac = $_GET['hmac'];

        // Sign the key with the nonce to get the tmp key
        $hmacKey = hash_hmac ( 'sha256', $sharedKey, $nonce );

        // rebuild the string to sign
        $localHmac = hash_hmac ( 'sha256', $userId , $hmacKey );

        // Compare against the incoming key
        $isValid = $localHmac == $hmac ? true : false;

    }
    return $isValid;
}

I'm aware of the replay attack vector and will be addressing it in due course.

Could you advise if this is secure? Is there anything else I might look at considering to achieve the same goal?


texts: Generating a unique reference number for logging purposes in a custom exception

texts:
I've come up with a base class for exceptions for this SMS-based service I am working on. The basic idea behind the service is that a user sends a text message to a number, the message requests a particular action to be carried out by an agent that logs into a different website, from which some account information is scraped and spat back out to the user. Small take on QoL. Obviously, things don't always work as intended for different reasons.

When something goes wrong and the returned text message doesn't contain the requested information, I want the user to have a reference number to that error, should the issue persist. To accommodate for that I will keep logs, where I will quickly be able to look up what happened using the reference number. The logs will contain full error information.

Intended use of the custom exceptions:

try:
    service.login(credentials)
except AuthenticationError as e:
    sms_client.send(phone_number, f'{Something went wrong. If this keeps on happening, please contact us with this error ref: {e.reference}')

...

import logging
import uuid

logging.basicConfig(filename='errors.log', level=logging.ERROR)

class ServiceException(Exception):
    def __init__(self, message, url):
        self.reference = str(uuid.uuid4()).split('-')[0]
        error_message = f'{message}: \'{url}\' | error ref: {self.reference}'
        logging.error(error_message)
        super(Exception, self).__init__(error_message)

class AuthenticationError(ServiceException):
    """Ambiguous error prevented successful authentication."""


texts: Program to automate skipping users on a chat website until the username meets certain conditions (v.2.0)

texts:
This is a python program that automates skipping through users on a chat website called talkwithstrangers using the Selenium Webdriver and BeautifulSoup modules of Python.

The first part uses Selenium to open the browser:

from selenium import webdriver
driver = webdriver.Chrome('C:\SeleniumDrivers\chromedriver.exe')
userName = 'null'
driver.get('https://talkwithstranger.com/talk-to-strangers')

The second part is a while loop that runs while the username has not met the required conditions:

while 'female' not in userName or 'Female' not in userName:
    driver.get('https://talkwithstranger.com/talk-to-strangers')
    html = driver.execute_script("return document.documentElement.innerHTML;")

    from bs4 import BeautifulSoup
    soup = BeautifulSoup(html, 'html5lib')
    userVal = soup.find(class_='alert text-center alert-success')
    userNames =  userVal.find_all('b')

    def getName(userNames):
        for name in userNames:
            return name.prettify()

    userName = getName(userNames)

The code is running fine but the problem is the efficiency and runtime.


texts: Performance of stable marriage solution in Python 3

texts:
I am trying to solve the stable marriage problem in SPOJ in Python 3.

  There are given \$n\$ men and \$n\$ women. Each woman ranks all men in order of her preference (her first choice, her second choice, and so on). Similarly, each man sorts all women according to his preference. The goal is to arrange \$n\$ marriages in such a way that if a man \$m\$ prefers some woman \$w\$ more than his wife, then \$w\$ likes her husband more than \$m\$. In this way, no one leaves his partner to marry somebody else. This problem always has a solution and your task is to find one.

  Input

  The first line contains a positive integer \$t \le 100\$ indicating the number of test cases. Each test case is an instance of the stable marriage problem defined above. The first line of each test case is a positive integer \$n \le 500\$ (the number of marriages to find). The next \$n\$ lines are the woman's preferences: \$i\$th line contains the number \$i\$ (which means that this is the list given by the \$i\$th woman) and the numbers of men (the first choice of \$i\$th woman, the second choice,...). Then, the men's preferences follow in the same format.

  Output

  For each test case print \$n\$ lines, where each line contains two numbers \$m\$ and \$w\$, which means that the man number \$m\$ and the woman number \$w\$ should get married.

I have tried optimising the code as much as I can (remove slicing, keep it minimal array, remove printing one by one... etc).

But by far the best code I have been able to get runs in 0.13(s?) time and 33M(??) memory. But the best code for the same problem in Python 3 (submitted by @_@) runs in 0.09 time and 13M memory. So I would like suggestions on how to attain the best time and space usage with my code

from sys import stdin, stdout

def findWoman(manArray, womanArray, index, mpref, wpref):
    for woman in mpref[index - 1]:
        if(woman == 0):
            continue
        hub = womanArray[woman - 1]
        if(hub == 0):
            womanArray[woman - 1] = index
            manArray[index - 1] = woman
            return 0
        elif(wpref[woman - 1].index(index) > wpref[woman - 1].index(hub)):
            continue
        else:
            manArray[hub - 1] = 0
            womanArray[woman - 1] = index
            manArray[index - 1] = woman
            return hub

out = ''
t = int(stdin.readline())
while(t > 0):
    t -= 1
    n = int(stdin.readline())
    mpref = []
    wpref = []
    for _ in range(0, n):
        w = list(map(int, stdin.readline().split()))
        w[0] = 0
        wpref.append(w)
    for _ in range(0, n):
        m = list(map(int, stdin.readline().split()))
        m[0] = 0
        mpref.append(m)
    manArray = [0 for _ in range(n)]
    womanArray = [0 for _ in range(n)]
    for k in range(n):
        hub = k + 1
        while(hub != 0):
            hub = findWoman(manArray, womanArray, hub, mpref, wpref)
    for k in range(n):
        out += str(k + 1) + ' ' + str(manArray[k]) + '\n'
stdout.write(out)


texts:
I have a users table where some users have the same name:

+----+------+
| id | name |
+----+------+
|  1 | Joe  |
|  2 | Joey |
|  3 | Joe  |
+----+------+

... and a purchases table:

+---------+--------+
| user_id | amount |
+---------+--------+
|       1 |    400 |
|       1 |    200 |
|       2 |    700 |
|       3 |    100 |
+---------+--------+

Goal

I want to show for each user, their name and the sum of their purchases like this (yes there are two people named Joe, no worry):

+------+-------+
| name | total |
+------+-------+
| Joe  |   600 |
| Joey |   700 |
| Joe  |   100 |
+------+-------+

My code

SELECT name, totals.total
FROM
  (
    SELECT user_id, SUM(amount) total
    FROM purchases
    GROUP BY user_id
  ) AS totals
INNER JOIN users u
ON totals.user_id=u.id;

It works, producing the right output.

How can I improve this code, in particular by not using a subquery?

Table creation code:

CREATE TABLE users (id INT UNIQUE NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(20));
INSERT INTO users (name) VALUES ('Joe'), ('Joey'), ('Joe');
CREATE TABLE purchases (user_id INT NOT NULL, amount INT);
INSERT INTO purchases (user_id, amount) VALUES (1, 400), (1, 200), (2, 700), (3, 100);


texts:
I am attempting to create a Tic-Tac-Toe game that uses a recursively checks every possible game board, and finds which boards result in a victory for X. (I ultimately plan to turn this into an AI which implements a minimax algorithm to play against a human player.) I am running into issues with the time efficiency of my code.

import time

X, O, NONE = ("X", "O", ".")

class Board():
    def __init__(self):
        self.board_length = 9;
        self.squares = [NONE for idx in range(self.board_length)]
        self.winning_combinations = [
            [0,1,2], [3,4,5], [6,7,8],
            [0,3,6], [1,4,7], [2,5,8],
            [0,4,8], [2,4,6]]

    def __str__(self):
        return  ("-------------\n| {} | {} | {} |\n" * 3 + "-------------").format(*self.squares)

    def make_move(self, location, player):
        if self.is_valid_move(location):
            self.squares[location] = player
            return True
        return False

    def undo_move(self, location):
        if self.is_valid_location(location):
            self.squares[location] = NONE
            return True
        return False

    def is_valid_move(self, location):
        return self.is_valid_location(location) and self.squares[location] == NONE

    def is_valid_location(self, location):
        return location >= 0 and location < self.board_length

    def get_winner(self):
        for player in (X, O):
            for combo in self.winning_combinations:
                if self.squares[combo[0]] == player and self.squares[combo[1]] == player and self.squares[combo[2]] == player:
                    return player
        return NONE

    def get_moves(self):
        return filter(self.is_valid_move, range(9))

iters = 0
def find_winners(board, depth=0, player=X):
    global iters
    iters += 1
    for move in board.get_moves():
        board.make_move(move, player)
        if board.get_winner() == X:
            #print(board)
            _ = 1
        else:
            find_winners(board, depth + 1, O if player == X else X)
        board.undo_move(move)

if __name__ == "__main__":
    start = time.time()
    find_winners(Board())
    print("Took: {time:.4f} seconds to execute {iters} times.".format(time=(time.time() - start), iters=iters))

When I run this code, I get an output of: Took: 8.4968 seconds to execute 986410 times.

Considering my code is only executing ~1m times, a time of almost 10 seconds to execute seems rather extreme. I can't find anything in my code that would particularly lead to this slowdown, or much that I can do to improve the efficiency of my code.

Any and all advice, on the efficiency of my code, or on my coding style in general, would be much appreciated.


texts:
As junior java programmer I am working with Euler project and I would like to create a general purpose class which generates all the combinations from the length and number of choosen elements. The class returns a list of boolean arrays with length of length. Each array has choosen true value which highlight the chosen elements the others are false. I have a working solution (I mean it is tested to small numbers) but I wonder is there any opportunity to improve the performance or improve some other aspect. Here it is:

public static List<boolean[]> generate(int length, int choosen){
    List<boolean[]> variationList = new ArrayList<>();
    boolean[] actual = initArray(length, choosen);
    variationList.add(actual.clone());
    while(incrementArray(actual)) {
        variationList.add(actual.clone());
    }
    return variationList;
}

private static boolean[] initArray(int length, int choosen) {
    boolean[] array = new boolean[length];
    for (int index=0; index<choosen; index++) {
        array[index] = true;
    }
    return array;
}

private static boolean incrementArray(boolean[] actual) {
    for(int index = actual.length-1; index > -1 ; index--) {
        if(actual[index]) {
            if(moveForward(actual, index)) {
                return true;
            }
        }
    }
    return false;
}

private static boolean moveForward(boolean[] actual, int position) {
    if(position<actual.length-1 && !actual[position+1]) {
        actual[position] = false;
        actual[position+1] = true;
        copyBack(actual, position);
        return true;
    }else {
        return false;
    }
}

private static boolean copyBack(boolean[] actual, int position) {
    boolean answer = false;
    int delay = 2;
    for (int index = position+2; index<actual.length;index++) {
        if(actual[index]) {
            actual[index] = false;
            actual[position + delay] = true;
            delay++;
            answer = true;
        }
    }
    return answer;
}

Edit

Euler project was only the motive I would like to find a way independently. My aim was produce all of the combinations (by the way my solution for the original problem needed them all (not just the number of them) but there is a workaround which no need combination at all).
Some example of the method:

CombinationGenerator.generate(5, 1);

The result:

true false false false false
false true false false false
false false true false false
false false false true false
false false false false true

Invocation:

CombinationGenerator.generate(5, 3)

The result:

true true true false false
true true false true false
true true false false true
true false true true false
true false true false true
true false false true true
false true true true false
false true true false true
false true false true true
false false true true true


texts:
I need to use sockets from winsok2.h. So, I written a class NetObject to use it. But, when I do compilation I am getting an error:

error LNK2019: unresolved external symbol "public: int __thiscall wsa::NetObject::connect(void)" (?connect@NetObject@wsa@@QAEHXZ) referenced in function _main

I do this using Visual C++ compiler.

Here is some code:

//wsa.h
#ifndef WSA_H_
#define WSA_H_

#include <winsock2.h>

namespace wsa {

sockaddr_in init(const int, const char *, const int);
sockaddr_in init(const int, const char *);
SOCKET init(const int, const int, const int);

class NetObject {
private:
    SOCKET sock;
    sockaddr_in addr;
    int check(const int);
public:
    NetObject
        (
            const int addr_family,
            const int sock_type,
            const int protocol,
            const char * ip_addr,
            const int port
        ): sock(init(addr_family, sock_type, protocol)),
           addr(init(addr_family, ip_addr, port)) {};
    int connect();
};
}

#endif //WSA_H_

//wsa.cpp
#include "wsa.h"
#include <winsock2.h>

namespace wsa {

sockaddr_in init(const int af, const char * ip_addr) {
    sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = af;
    addr.sin_addr.s_addr = inet_addr(ip_addr);
    return addr;
}

sockaddr_in init(const int af, const char * ip_addr, const int port) {
    sockaddr_in addr = init(af, ip_addr);
    addr.sin_port = htons(port);
    return addr;
}

SOCKET init
    (
        const int af,
        const int type,
        const int protocol
    ) {

    SOCKET s;
    s = socket(af, type, protocol);
    while (s == INVALID_SOCKET) {
        if (closesocket(s) == SOCKET_ERROR) {
            return INVALID_SOCKET;
        }
        s = socket(af, type, protocol);
    }
    return s;
}

inline int NetObject::check(const int code) {
    if (code == SOCKET_ERROR) {
        if (closesocket(sock) == SOCKET_ERROR)
            return 1;
        return SOCKET_ERROR;
    }
    return 0;
}

inline int NetObject::connect() {
    int res = ::connect(sock, (sockaddr *)&addr, sizeof(addr));
    return check(res);
}
}

//main.cpp
#include "wsa.h"
#include <winsock2.h>

const int af       = AF_INET;
const int type     = SOCK_STREAM;
const int protocol = IPPROTO_TCP;

int main(int argc, char * argv[]) {

    WSADATA ws;
    if (WSAStartup(MAKEWORD(2, 0), &ws) == -1) {
        return 1;
    }

    wsa::NetObject server(af, type, protocol, argv[1], 80);
    server.connect();
    WSACleanup();
}

I can't see any errors. Please, help!


texts: Dynamic social media component w/ dark mode

texts:
Note: This is my first post for a code review, so any feedback on what other info would be helpful to post (if any) would be much appreciated!
React component: Social.js
The component has an input of 4 properties:

dark: Boolean - if dark is supplied, the component renders white icons for contrast against a dark background
facebook: String - if facebook url is supplied, the component renders the facebook icon, else no icon at all
twitter: String - if twitter url is supplied, the component renders the twitter icon, else no icon at all
instagram: String - if instagram url is supplied, the component renders the instagram icon, else no icon at all

If only 1 social media url is supplied, the text "Follow us on: " is rendered along with the single icon
let count = 0;

function countProp (prop) {
  if (prop !== undefined) {
    if (prop.url !== ``) {
      count++;
    }
  }
  return prop;
}

const Social = props => {
  count = 0;
  let facebook = countProp(props.facebook);
  let twitter = countProp(props.twitter);
  let instagram = countProp(props.instagram);
  return (
    <>

      { (count == 1) ? Follow us on: : `` }

      { (!props.dark) ?
                ( (facebook.url !== ``) ?

                  : null )
                : ( (facebook.url !== ``) ?

                  : null ) }

      { (!props.dark) ?
                ( (twitter.url !== ``) ?

                  : null )
                : ( (twitter.url !== ``) ?

                  : null ) }

      { (!props.dark) ?
                ( (instagram.url !== ``) ?

                  : null )
                : ( (instagram.url !== ``) ?

                  : null ) }

    </>

)};

export default Social;


texts: OutputStream class for use with subprocess.Popen

texts:
I've recently been writing an application which does a lot of work with the output of commands.  To call the commands I've been using the subprocess module, and originally, was using subprocess.PIPEs for the stdout and stderr, and then using the communicate method once the process was complete to retrieve the output to work with it, log it, etc.  Unfortunately, I had a problem with deadlocks when a particular command generated a long output stream.

For this reason, I've written an OutputStream class, an instance of which can be provided as the stdout and stderr params of subprocess.Popen.  In the background, it writes to a cStringIO buffer which I can retrieve the value from later, and it seems to work well.  However, this is the first time I've worked with anything like this and I'm concerned that I might be doing something potentially unsafe.  I'd appreciate any feedback anybody has.

from cStringIO import StringIO
import os
import threading

class OutputStream(threading.Thread):
    def __init__(self):
        super(OutputStream, self).__init__()
        self.done = False
        self.buffer = StringIO()
        self.read, self.write = os.pipe()
        self.reader = os.fdopen(self.read)
        self.start()

    def fileno(self):
        return self.write

    def run(self):
        while not self.done:
            self.buffer.write(self.reader.readline())

        self.reader.close()

    def close(self):
        self.done = True
        os.close(self.write)

I would then use it something like this:

import subprocess
import time

stream = OutputStream()
proc = subprocess.Popen(['ls', '-l'], stdout=stream, stderr=subprocess.STDOUT)

while proc.poll() is None:
    time.sleep(0.05)

stream.close()
output = stream.buffer.getvalue()
print output

Edit: When running the test code above, Python doesn't exit once the command output has been printed.  Such behaviour isn't exhibited by my application in which this class is being used.


texts: Batch gradient descent and stochastic gradient descent

texts:
I'm trying to implement logistic regression and I believe my batch gradient descent is correct or at least it works well enough to give me decent accuracy for the dataset I'm using. When I use stochastic gradient descent I'm getting really poor accuracy so I'm not sure if it's my learning rate, epochs or just my code is incorrect. Also I'm wondering how would I add regularization to both of these? Do I add a variable lambda and multiply it by the learning rate or is the more to it?

BGD:

def batch_gradient(df, weights, bias, lr, epochs):
    X = df.values
    y = X[:,:1]
    X = X[:,1:]
    length = X.shape[0]
    for i in range(epochs):
        output = (sigmoid((np.dot(weights, X.T)+bias)))
        weights_tmp = (1/length) * (np.dot(X.T, (output - y.T).T))
        bias_tmp = (1/length) * (np.sum(output - y.T))

        weights -= (lr * (weights_tmp.T))
        bias -= (lr * bias_tmp)

    return weights, bias

SGD:

def stochastic_gradient(df, weights, bias, lr, epochs):
    x_matrix = df.values
    for i in range(epochs):
        np.random.shuffle(x_matrix)
        x_instance = x_matrix[np.random.choice(x_matrix.shape[0], 1, replace=True)]
        y = x_instance[:,:1]

        output = sigmoid(np.dot(weights, x_instance[:,1:].T) + bias)
        weights_tmp = lr * np.dot(x_instance[:,1:].T, ((output - y)))

        weights = (weights - weights_tmp.T)
        bias -= lr * (output - y)

    return weights, bias


texts:
Im trying to draw a lot of fields image for my world generation, all is perfect 64 fps, but if i began to draw all the time the grass it became laggy, like 59 fps (something that musn't happen! :( ), i need help and some tricks to draw things in c# with more optimization :)

int loadedChunks = 0;
        for (int chunkX = 0; chunkX < sizeX; chunkX++)
        {
            for (int chunkY = 0; chunkY < sizeY; chunkY++)
            {
                int xChunk = chunkX + CamXCh - 1,
                    yChunk = chunkY + CamYCh - 1;

                long idChunk = xChunk + ((long)yChunk << 32);

                if (allChunks.ContainsKey(idChunk))
                {
                    loadedChunks++;
                    Chunks currentChunk = allChunks[idChunk];
                    List<CeldsChunk> celds = currentChunk.MapChunk;

                    // Draw Ground
                    for(int celdIndex = 0; celdIndex < celds.Count; celdIndex++)
                    {
                        CeldsChunk celd = celds[celdIndex];
                        Point cord = celd.cords;
                        int sizeCL = cord.size,
                            CeldX = xChunk * sizeChunk + CameraX + cord.posX * sizeCL,
                            CeldY = yChunk * sizeChunk + CameraY + cord.posY * sizeCL,
                            sizeCeld = cord.size;

                        Color celdType = celd.type;
                        //SolidBrush brsh = new SolidBrush(celdType);
                        g.DrawImage(grass, new Rectangle(CeldX, CeldY, sizeCeld, sizeCeld));

                        //Image sk = celd.img;
                        //if (sk != null)
                        //    step2.Add(new Point(CeldX, CeldY, sizeCeld), sk);
                    }
                }
            }
        }

        // Step 2  Draw Forest
        foreach (KeyValuePair<Point, Image> s in step2)
        {
            Point kps = s.Key;
            Image ips = s.Value;
            int posX = kps.posX,
                posY = kps.posY,
                siz = s.Key.size,
                w = ips.Width,
                h = ips.Height;

            //g.DrawRectangle(Pens.Red, new Rectangle(posX, posY, siz, siz));
            //g.DrawRectangle(Pens.Blue, new Rectangle(posX - (h >> 2) + (siz >> 1), posY - h + siz, w, h));
            g.DrawImage(ips, new Rectangle(posX - (h >> 2) + (siz >> 1), posY - h + siz, w, h));
        }


texts:
Here I'm trying to develop a simple framework for my own purpose. I'm using PHP and PDO. But, I'm still worried about my password security codes. Below I have posted my codes. it stored like these in my database $1$sr1.F90.$hXd.9DheSMtYmI52KT7fy0. Is this the right way to store passwords? Or Am I make anything wrong here?

I am worried about SQL injection and XSS attacks. But, here I'm using PDO. Is this code good enough to prevent attacks?

Insert script

$password = $_POST['pwd'];
$crypted_pass = crypt($password);
date_default_timezone_set('Asia/Kolkata');
$date = date('Y-m-d h:i:s');
$insert_query = $dbh->prepare("INSERT INTO admin ( user_name, user_pwd, user_email, profile_created_on ) VALUES ( :username, :userpwd, :useremail, :date )");
$insert_query->bindValue(':username', 'Administrator');
$insert_query->bindValue(':userpwd', $crypted_pass);
$insert_query->bindValue(':useremail', 'something@gmail.com');
$insert_query->bindParam(':date', $date);
$insert_query->execute();

Access script

if(isset($_POST['admin_login']))
{
        $username = mysql_real_escape_string($_POST['admin_user_name']);
        $password = mysql_real_escape_string($_POST['admin_user_pwd']);
        try
        {
            $select_query = $dbh->prepare("SELECT * FROM admin WHERE user_name = '$username'");
            $result = $select_query->execute();
            if(count($result) > 0)
            {
                while($row = $select_query->fetch(PDO::FETCH_ASSOC))
                {
                    if(crypt($password, $row['user_pwd']) == $row['user_pwd'] && $row['user_name'] == $username)
                    {
                        session_start();
                        $_SESSION['sessionname'] = $row['user_name'];
                        header('Location: dashboard.php');
                        return true;
                    }
                    else
                    {
                        echo '
Access Denied. Wrong Username or Password. Go Back
';
                        return false;
                    }
                }
            }
        }


texts: Optimization of region of interest (ROI) drawing in the image

texts:
I'm drawing rectangular ROI region by darkening the area outside of it as in this image:

But image.MakeTransparent takes too much time. What is the best way to increase drawing speed?

void DrawRoi(Bitmap Image, RectangleF rect)
{
        Rectangle roi = new Rectangle();

        roi.X = (int)((float)Image.Width * rect.X);
        roi.Y = (int)((float)Image.Height * rect.Y);
        roi.Width = (int)((float)Image.Width * rect.Width);
        roi.Height = (int)((float)Image.Height * rect.Height);

        Stopwatch timer = new Stopwatch();
        timer.Start();
        // graphics manipulation takes about 240ms on 1080p image
        using (Bitmap roiMaskImage = CreateRoiMaskImage(ImageWithRoi.Width, ImageWithRoi.Height, roi))
        {
                using (Graphics g = Graphics.FromImage(ImageWithRoi))
                {
                        g.DrawImage(Image, 0, 0);
                        g.DrawImage(roiMaskImage, 0, 0);
                        Pen borderPen = CreateRoiBorderPen(ImageWithRoi);
                        g.DrawRectangle(borderPen, roi);
                }
        }
        Debug.WriteLine("roi graphics: {0}ms", timer.ElapsedMilliseconds);
        this.imagePictureBox.Image = ImageWithRoi;
}

Bitmap CreateRoiMaskImage(int width, int height, Rectangle roi)
{
        Bitmap image = new Bitmap(width, height, PixelFormat.Format32bppArgb);
        using (Graphics g = Graphics.FromImage(image))
        {
                SolidBrush dimBrush = new SolidBrush(Color.FromArgb(64, 0, 0, 0));
                g.FillRectangle(dimBrush, 0, 0, width, height);
                SolidBrush roiBrush = new SolidBrush(Color.Red);
                g.FillRectangle(roiBrush, roi);
                image.MakeTransparent(Color.Red);
                return image;
        }
}

Pen CreateRoiBorderPen(Bitmap image)
{
        float width = ((float)(image.Width + image.Height) * 2.5f) / (float)(640 + 480);
        if (width < 1.0f)
                width = 1.0f;
        Pen pen = new Pen(Color.FromArgb(255, 0, 255, 0), width);
        return pen;
}


texts: Code to replace list of integers with nearest bigger element

texts:
The original question was in the context of an array, but I just used a list without changing the length for easier debugging purposes of toString().

  Given an unsorted list of integers, design a \$O(n)\$ algorithm to transform the list such that the integers are replaced by the next bigger integer on their right. If there is no bigger integer on its right, the integer remains the same.

  For example \$[2,1,4,5,3,6,7,9,4,8]\$ becomes \$[4,4,5,6,6,7,9,9,8,8]\$.

I have come out with an algorithm but I'm not really confident whether it is considered \$O(n)\$ and where I can improve on if it really is \$O(n)\$. Can anyone provide me with some feedback?

import java.util.*;

class Main {
    public static void main(String [] args) {
        ArrayList<Integer> lst = new ArrayList<>(List.of(2,1,4,5,3,6,7,9,4,8));
        replace2(lst);
        System.out.println(lst);
    }

    //O(n^2) algorithm
    public static void replace(int [] arr) {
        for (int i=0;i< arr.length;i++) {
            for (int j=i;j<arr.length;j++) {
                if (arr[i]<arr[j]) {
                    arr[i]= arr[j];
                    break;
                }
            }
        }
    }

    //O(n) algorithm
    public static void replace2(ArrayList<Integer> lst) {
        int index = 0;
        int seen =0;
        for (int i=0;i< lst.size()-1;i++) {
            boolean isBroken = false;
            if (lst.get(i) >= lst.get(i+1)) {//if the adjacent element is larger, find for next biggest
                for (int j=i;j < lst.size(); j++) {
                    if (lst.get(j) > lst.get(i)) {
                        index = j;
                        seen = lst.get(j);
                        isBroken = true;
                        break;
                    }
                }
                if (isBroken) {//if we have found the next biggest
                    for (int k=index-1;k>i-1;k--) {
                        if (lst.get(k) < seen) {
                            if (lst.get(k) < lst.get(i)) {
                                lst.set(k,seen);
                            } else {
                                int temp = lst.get(k);
                                lst.set(k,seen);
                                seen = temp;
                            }
                        }
                    }
                }

            } else {//the adjacent element is already bigger
                lst.set(i,lst.get(i+1));
            }
        }
    }
}


texts: Advent of Code 2017 Day 5 (part 1) in Functional Programming (FP)

texts:
I wanted to practice functional programming (FP) without using any library but using vanilla JS only. So I took a problem from Advent of Code (the 1st part of Day 5):

  An urgent interrupt arrives from the CPU: it's trapped in a maze of
  jump instructions, and it would like assistance from any programs with
  spare cycles to help find the exit.

  The message includes a list of the offsets for each jump. Jumps are
  relative: -1 moves to the previous instruction, and 2 skips the next
  one. Start at the first instruction in the list. The goal is to follow
  the jumps until one leads outside the list.

  In addition, these instructions are a little strange; after each jump,
  the offset of that instruction increases by 1. So, if you come across
  an offset of 3, you would move three instructions forward, but change
  it to a 4 for the next time it is encountered.

  For example, consider the following list of jump offsets:

0
3
0
1
-3

  Positive jumps ("forward") move downward; negative jumps move upward.
  For legibility in this example, these offset values will be written
  all on one line, with the current instruction marked in parentheses.
  The following steps would be taken before an exit is found:

  (0) 3  0  1  -3  - before we have taken any steps.
  (1) 3  0  1  -3  - jump with offset 0 (that is, don't jump at all). Fortunately, the instruction is then incremented to 1.
  2 (3) 0  1  -3  - step forward because of the instruction we just modified. The first instruction is incremented again, now to 2.
  2  4  0  1 (-3) - jump all the way to the end; leave a 4 behind.
  2 (4) 0  1  -2  - go back to where we just were; increment -3 to -2.
  2  5  0  1  -2  - jump 4 steps forward, escaping the maze.

  In this example, the exit is reached in 5 steps.

  How many steps does it take to reach the exit? Input :
  https://adventofcode.com/2017/day/5/input

My solution in FP:

  const parsedInput = input => input
    .split('\n')
    .map(c => parseInt(c));

  const jumper = input => {
    const currentMaze = input.maze;
    const currentPosition = input.position;
    const currentStepNumber = input.stepNumber;
    if (currentPosition >= currentMaze.length) {
      return currentStepNumber;
    }
    const newOffset = currentMaze[currentPosition] + 1;
    const maze = currentMaze
     .slice(0, currentPosition)
     .concat(newOffset)
     .concat(currentMaze.slice(currentPosition + 1));
    const position = currentPosition + currentMaze[currentPosition];
    const stepNumber = currentStepNumber + 1;
    return jumper({
      maze, position, stepNumber
    });
  };

  const startMaze = parsedInput(INPUT);
  const solution = jumper({
    maze: startMaze,
    position: 0,
    stepNumber: 0
  });

  console.log("solution ", solution);

Running this code with node9 in normal mode will create a RangeError: Maximum call stack size exceeded. I had to "cheat" by running this code in node 6: node --harmony 05_1.js (node 6 supports Tail Cail Optimization (TCO) when the flag --harmony is turned on). I also tried to cache the maze but this didn't help either. I got the feeling that big size Input is the limit for FP.


texts: Executing test cases when executing the SQL script

texts:
While executing a SQL script in production like sensitive database, usually I do the following

BEGIN TRANSACTION

--Declare of test variables
DECLARE @ExpectedAffectedRows INT = 10 -- Manually count this
DECLARE @ActualAffectedRows INT = 0

-- Its a demo code, don't take it seriously ;P
DELETE * FROM SomeTable AS st WHERE st.ColumnA = 'A'
SET @ActualAffectedRows = @ActualAffectedRows + @@ROWCOUNT

UPDATE SomeTable
SET ColumnA = 'A' WHERE ColumnA='B'
SET @ActualAffectedRows = @ActualAffectedRows + @@ROWCOUNT

IF @@ERROR<>0 OR @ActualAffectedRows<>@ExpectedAffectedRows
BEGIN
    ROLLBACK
    SET NOEXEC ON
END

IF @@TRANCOUNT>0
BEGIN
    COMMIT
END

The thing here, I hard coded the @ExpectedAffectedRows variable manually, counting how many rows will be affected.

Is the process fine or are there other superior processes or best practices to deal with this type of data update/delete things?


texts:
Any major security risks? And please don't get angry over my novice log system.

<?php
   function makesalt($lg)
    {
    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-';
    $sz = strlen( $chars );
    $str = '';
    for( $i = 0; $i < $lg; $i++ ) {
        $str .= $chars[ rand( 0, $sz - 1 ) ];
    }
    return $str;
  }
  function duelsha($th)
  {
      $tem = hash('sha512', $th);
      $tem2 = md5($tem);
      return hash('sha256', $tem2);
  }
  function shasalt($ht)
  {
      $salt = makesalt(30);
      return duelsha($ht . $salt) . '|' . $salt;
  }
  function mysqlsan($ss)
  {
      if (get_magic_quotes_gpc()) $ss = stripslashes($ss);
      return mysql_real_escape_string($ss);
  }
  function htent($st)
  {
      return htmlentities($st);
  }
  function sqlhtml($sm)
  {
       return htent(mysqlsan($sm));
  }
  function logs($tl)
  {
    $fh = fopen("server.log", 'w') or die("File error");
    fwrite($fh, $tl) or die("File error");
    fclose($fh);
  }
?>


texts:
My goal was to create a simple-to-use CLI program for drawing directory trees (what tree does on most platforms, basically). I'm submitting my 'backend' program for review because I think it makes more sense to review the actual algorithm, rather than reviewing an interface on top of it.

import os
import re

def tree(path, indentation=4, ignore_hidden=True, regex=None):
    """Build a directory tree starting at `path`. See also
    https://en.wikipedia.org/wiki/File:Files11_directory_hierarchy.svg

    Arguments
    * path: A relative or absolute path to use as starting point
            for the tree. [type: str]

    * indentation: The amount of spaces to indent each level with.
                   Defaults to 4. [type: int]

    * ignore_hidden: Ignore hidden files (starting with a period).
                     Defaults to True. [type: boolean]

    * regex: If set, only matching files will be shown.
             Set to None to show all files. Defaults to None.
             [type: str / None]

    Returns
    * A directory tree [type: str]

    Example
    Assuming we're in the 'home' directory:

        >>> import tree
        >>> t = tree.tree(path="./")
        >>> t

        ./images
            foo.jpg
            bar.jpg
            ./images/personal
                spam.png
        ./documents
            eggs.docx

        >>> t = tree.tree(path="./", ignore_hidden=False)
        >>> t

        .hidden.txt
        ./images
            foo.jpg
            bar.jpg
            .hidden.jpg
            ./images/personal
                spam.png
        ./documents
            eggs.docx

        >>> t = tree.tree("./", 8, regex="(.*\.png)")
        >>> t

        ./images
                ./images/personal
                        spam.png
        ./documents
    """
    structure = "\n"
    tab = " "*indentation
    if regex is None:
        for root, directories, files in os.walk(path):
            try:
                depth = root.count(os.sep)
                offset = tab * depth
                structure += offset + root + "\n"
            except OSError:
                continue
            for f in files:
                if ignore_hidden and f.startswith("."):
                    continue
                try:
                    depth = os.path.join(root, f).count(os.sep)
                    offset = tab * depth
                    structure += offset + f + "\n"
                except OSError:
                    continue
    else:
        restriction = re.compile(regex)
        for root, directories, files in os.walk(path):
            try:
                depth = root.count(os.sep)
                offset = tab * depth
                structure += offset + root + "\n"
            except OSError:
                continue
            for f in files:
                if ignore_hidden and f.startswith("."):
                    continue
                if not re.match(restriction, f):
                    continue
                try:
                    depth = os.path.join(root, f).count(os.sep)
                    offset = tab * depth
                    structure += offset + f + "\n"
                except OSError:
                    continue
    structure = structure.split("\n")
    dedented = list(map(lambda e: e.replace(tab, "", 1), structure))[1:]
    return "\n".join(dedented)

I believe my code runs in \$O(n)\$ complexity and running it on my Android phone on / takes about 9 seconds:

time python -c "import tree; tree.tree('/')"

real    0m9.365s
user    0m7.390s
sys     0m1.630s

I'd appreciate any feedback regarding performance, usability, coding style, documentation, or anything else!


texts: How can use array to imitate repeated individual instances?

texts:
I'm working an example to list directory contents. When I process each directory the code works. Now I'd like to put both directories in an array and iterate the array. I get an error when I declare 'dir_c' because I've not given it a directory name which will change.

  def outfile = new File('Y:/TEMP/dirlist.txt')
  def dir_a = new File('z:/pathA/pathB')
  def dir_b = new File('z:/pathA/pathQ')
  // def dir_c = new File() //causes error ==>Could not find which method <init>(to invoke from this list
outfile.write('')

 dir_a.eachFile {
  if (it.isFile()) {
  outfile.append(it.name + '\n')
  }

  dir_b.eachFile {
   if (it.isFile()) {
    outfile.append(it.name + '\n')
   }
  // Up to this point code works

   def searchDir = ["z:/pathA/pathB","z:/pathA/pathQ"]
   searchDir.each{
  //was thinking using  ==>dir_c = it
  // then carry on like singleton cases
  // but I get error message when I declare dir_c
  }


texts:
I've just solved this challenge, though I feel that there is a problem with stack growth or something because the 5th testcase has a runtime error

import Control.Monad
import Data.Sequence hiding (replicateM, replicate, take, drop)
import Data.List

main :: IO ()
main = do n <- getLine
          input <- getLine
          putStrLn (unwords (map show (calc (map read (words input)) (output))))

output :: [Int]
output = replicate 100 0

calc :: [Int] -> [Int] -> [Int]
calc [] y      = y
calc (x:xs) ys = calc xs ((take (x) ys) ++ ((ys!!x) + 1):(drop (x+1) ys))

I feel that there should be a much neater solution, which doesn't cause an overflow as this is quite messy.

It would be nice to get everyone's thoughts on more efficient/nicer solutions


texts: Nested If, working like an excel SUMIF for two unequal lists summing the distance if GPS timestamp meets criteria

texts:
This is my first post, I am very new to coding and Python especially,

This code intends to do an excel SUMIF between two tables with different indexes. The first tables has GPS data with timestamp, vehicle ID and distance The second table has vehicle ID and timestamps of events I want to measure the distance run during events

Thanks

for x in range(1,34):
df = pd.read_csv("file"
                 + str(x) + '.csv',
                 parse_dates=[10])

red = 0
green = 0
black = 0

output = [[], [], [], []]
for i in range(len(lista[1])):
    for j in range(len(listc[1])):
        if listc[1][j] <= lista[3][i] or listc[1][j] >= lista[2][i]:
            if lista[7][i] >= listc[1][j] and lista[6][i] <= listc[1][j] and lista[0][i] == listc[0][j] and lista[8][i] == 'intended value' :
                red += listc[2][i]
            if lista[3][i] >= listc[1][j] and lista[7][i] <= listc[1][j] and lista[0][i] == listc[0][j] and lista[8][i] != 'intended value' :
                red += listc[2][i]
            if lista[6][i] >= listc[1][j] and lista[2][i] <= listc[1][j] and lista[0][i] == listc[0][j] and lista[8][i] == 'intended value' :
                green += listc[2][i]
            if lista[7][i] >= listc[1][j] and lista[2][i] <= listc[1][j] and lista[0][i] == listc[0][j] and lista[8][i] != 'intended value' :
                green += listc[2][i]
            if lista[2][i] >= listc[1][j] and lista[3][i - 1] <= listc[1][j] and lista[0][i] == listc[0][j]:
                black += listc[2][i]
    toc = timeit.default_timer()
    if i % 100 == 0:
        print('processing algorithm: {}'.format(toc - tic))
        print('we are at row {}'.format(i))
    output[0].append(lista[1][i])
    output[1].append(red)
    output[2].append(green)
    output[3].append(black)
    red = 0
    green = 0
    black = 0
toc = timeit.default_timer()
np.savetxt("outfile" + str(x)
           + ".csv", np.column_stack((output[0], output[1], output[2], output[3])), delimiter=",", fmt='%s')
tac = timeit.default_timer()
print('exporting {}'.format(tac - toc))


texts: Scraping SEDE query results with caching

texts:
I use this script to scrape the results of a SEDE page and return as a BeautifulSoup object.

A small twist is that if I don't use a SEDE query manually in the browser for a few days, then non-interactive downloads get empty results. (I suspect there is a robot test there.)

Rather than fixing the robot test issue, my workaround is to cache successfully downloaded pages, and use the cache when downloading is not working well.

import logging
import os

import requests
from bs4 import BeautifulSoup

BASE_DIR = os.path.dirname(__file__)
CACHE_DIR = os.path.join(BASE_DIR, '.cache')

def fetch_sede_soup(label, url):
    def is_valid(soup):
        for script in soup.findAll('script'):
            if 'resultSets' in script.text:
                return True
        return False

    if not os.path.isdir(CACHE_DIR):
        os.mkdir(CACHE_DIR)

    logging.info('fetching {} as {}'.format(label, url))
    html = requests.get(url).text
    soup = BeautifulSoup(html)

    cache_path = os.path.join(CACHE_DIR, '{}.html'.format(label))
    debug_cache_path = os.path.join(CACHE_DIR, '{}-debug.html'.format(label))

    if is_valid(soup):
        logging.info('updating cache')
        with open(cache_path, 'w') as fh:
            fh.write(html)
        return soup

    with open(debug_cache_path, 'w') as fh:
        fh.write(html)

    logging.warning('result not valid')
    if os.path.exists(cache_path):
        logging.info('using previous cache')
        with open(cache_path) as fh:
            return BeautifulSoup(fh)

Can this be written better?

In case it helps, here are some sample output files:

sede-output.html - a successful download.
sede-output-debug.html - a "failed" download (empty results).


texts: Function returning a vec with prime factors of number

texts:
See code below and my comment with questions:
pub fn factors(n: u64) -> Vec<u64> {
    let mut m = n; // is it possible to convert n to a mutable type without using another variable?
    let mut a:Vec<u64> = Vec::new();
    while m % 2 == 0 {
        m /= 2;
        a.push(2);
    }
    // in C we can just write for (int i = 3; i <= sqrt(m); i+= 2) ... just saying... and C is not even as fast as python to code... am I really doing it right??
    for i in (3..((m as f64).sqrt() as usize + 1usize)).step_by(2) {
        while m % i as u64 == 0 { // is there a way to avoid all the casts to u64
            a.push(i as u64);
            m /= i as u64;
        }
    }
    if m > 2 {
        a.push(m);
    }
    return a;
}

Is it possible to convert n to a mutable type without using another variable?
The C equivalent of the main loop use twice less ink. Is Rust so verbose by design?
Is there a way to avoid all the casts to u64
any other insight welcome


texts: ChangeCalculator for calculating money denominations in change

texts:
I'm doing some exercises in OOP and I just created a program that calculates amounts of denominations in a change at the shop. So, let's say something costs 10, I give 25.30 (for some reason), so I should get as a change: one 10, one 5, one 0.20, one 0.10.

When I started to create the program, I thought it would be nice if client of the ChangeCalculator had an option to specify his way of calculating the amounts - for example to use his own currency. That's why I created interfaces.

My program consists of:

IChangeCalculator - represents change calculator
ChangeCalculatorPLN - implementation that returns MoneyAmountPLN (money denominations amounts in PLN currency)
IMoneyAmount - represents data about amounts of denominations
MoneyAmountPLN - represents money denominations amounts in PLN
IMoneyToCoinsConverter - represents converter that converts given money to something that implements IMoneyAmount
MoneyToCoinsConverterPLN - converts given amount of money to MoneyAmountPLN

Here is the source code:

public interface IChangeCalculator<T> where T : IMoneyAmount
{
    T Calculate(decimal price, decimal payedAmount);
}

public class ChangeCalculatorPLN : IChangeCalculator<MoneyAmountPLN>
{
    IMoneyToCoinsConverter<MoneyAmountPLN> _moneyToCoinsConverter;

    public ChangeCalculatorPLN( IMoneyToCoinsConverter<MoneyAmountPLN> moneyToCoinsConverter )
    {
        _moneyToCoinsConverter = moneyToCoinsConverter;
    }

    public MoneyAmountPLN Calculate(decimal price, decimal payedAmount)
    {
        var change = payedAmount - price;
        if (change < 0)
            throw new Exception("The given amount of money is not enough!");
        if (change == 0)
            return MoneyAmountPLN.Empty;

        var amount = GetAmount(change);
        return amount;
    }

    private MoneyAmountPLN GetAmount(decimal change)
    {
        return _moneyToCoinsConverter.Convert(change);
    }
}

public interface IMoneyAmount
{
    IEnumerable<int> GetAmounts( );
}

//MoneyAmountPLN is quite long, because it contains fields for each denomination: 500, 200, 100, 50, 20, 10, 1, 0.5, 0.2, 0.1, 0.05, 0.02, 0.01. I'll not put it here.

public interface IMoneyToCoinsConverter<T> where T : IMoneyAmount
{
    T Convert(decimal money);
}

public class MoneyToCoinsConverterPLN : IMoneyToCoinsConverter<MoneyAmountPLN>
{
    public MoneyAmountPLN Convert(decimal money)
    {
        //logic doesn't really matter here. It returns MoneyAmountPLN with corrects amounts of denominations.
    }

}

The program works, I did some unit tests. My real issue is that I'm not sure if this implementation is a good one. Did I do some mistakes? Could some parts be done better? Personally I think that maybe the structure is a little too complicated? If I saw a program like this for the first time, I would probably feel lost, but I'm not a very good programmer, so I can't say if my feeling is right.
Adding new currency requires creation of new classes that implement: IChangeCalculator, IMoneyAmount, IMoneyToCoinsConverter.
I think that this is too much. I think that ChangeCalculator shouldn't need to be reimplemented for each new currency, but I didn't know how to do that, since each currency requires different IMoneyAmount implementation. Calculate method of ChangeCalculator needs to know what type of IMoneyAmount it returns.

Please provide constructive feedback, it would be great to hear any of your advices.


texts: Splitting a sequence into equal segments

texts:
I need to split up a sequence into equal segments of a given size (yes, the last one may be shorter), and I'm trying to find an efficient and idiomatic way to do it.

I have two versions of the function; segment is the F# version of an awkward and probably quite inefficient old C# extension method that I wrote, while chop is an attempt to do it in a more intelligent manner, but is still rather imperative. I would have liked to have a solution that doesn't need to use reference cells, but could not come up with one.

module Seq =
    let segment segmentSize source =
        seq {
                let segmentCount = ref 0
                while !segmentCount * segmentSize < Seq.length source
                    do
                        let segment = source |> Seq.skip (!segmentCount * segmentSize) |> Seq.truncate segmentSize
                        segmentCount := !segmentCount + 1
                        yield segment
        }

    let chop segmentSize source =
        seq {
                let chopSource = ref source
                while not <| Seq.isEmpty !chopSource
                    do
                        let segment = !chopSource |> Seq.truncate segmentSize
                        chopSource := !chopSource |> Seq.skip (Seq.length segment)
                        yield segment
        }

As I said, I think segment, while it gets the job done, is far from good F#; how efficient and idiomatic is chop? What could have been done better?

As for naming, is chop or segment the better name for a function like this, or would something else be more fitting? Also, the argument name segmentSize seems a bit off for F#; would just size be better, or something else?


texts: Moving a div inside p to the body element

texts:
I get malformed HTML input with divs inside other HTML elements like the html_string variable in the below code. As this is not valid HTML, parsers give me unexpected results and it can't be used logically.

I am trying to fix this HTML by reading it as an XML initially using BeautifulSoup and then re-positioning the div to the body element which is where it belongs in my case.

Understanding that this is a valid XML but invalid HTML is one of the the keys to this solution. Can anyone review this code?

This is used to fix page-break divs that come from a certain source and it is not a regular HTML.

import bs4

html_string = """

  <title></title>

  This is before.

  This is after.

"""

html_element = bs4.BeautifulSoup(html_string, features="xml")

style = {'style': 'page-break-after:always'}

page_break_elements = html_element.findAll('div', style)

for page_break_element in page_break_elements:
    current = page_break_element
    while True:
        parent = current.parent
        if parent is None:
            break
        if parent.name == 'body':
            current.insert_before(page_break_element)
            break
        current = parent


texts: Writing billions of lines to text file in java

texts:
I'm trying to write billions of strings lines to a file, it works for up to 40 million lines, but it's throwing out "java.nio.BufferOverflowException" error for 400 million lines. I also think my solution is slow as it takes 45 seconds to write 40 million lines. Below is the code.

public static void main(String[] args) throws FileNotFoundException, IOException {

byte[] buffer = "Help I am trapped in a fortune cookie factory\n".getBytes();
int number_of_lines = 400000000;

FileChannel rwChannel = new RandomAccessFile("textfile.txt", "rw").getChannel();
ByteBuffer wrBuf = rwChannel.map(FileChannel.MapMode.READ_WRITE, 0, buffer.length * number_of_lines);
for (int i = 0; i < number_of_lines; i++) {
    wrBuf.put(buffer);
}
rwChannel.close();
}

How can I make it be faster if possible and also write more than 2 billion lines of string?


texts: Generating combinations of specific values of a nested dict

texts:
I am developing a framework that allows to specify a machine learning model via a yaml file with different parameters nested so the configuration files are easy to read for humans.

I would like to give users the option of instead of specifying a parameter giving a range of options to try via a list.

Then I have to take this and generate all the possible valid combinations for the parameters the user has given multiple values for.

To mark which parameters are in fact lists and which ones are multiple values, I have opted to choose that combination values begin with 'multi_' (though if you have a different take I would be interested to hear it!).

So for example an user could write:

config = {
    'train_config': {'param1': 1, 'param2': [1,2,3], 'multi_param3':[2,3,4]},
    'model_config': {'cnn_layers': [{'units':3},{'units':4}], 'multi_param4': [[1,2], [3,4]]}
}

Indicating that 6 configuration files must be generated, where the values of 'param3' and 'param4' take all the possible combinations.

I have written a generator function to do this:

from pandas.io.json.normalize import nested_to_record
import itertools
import operator
from functools import reduce

from collections import MutableMapping
from contextlib import suppress

def generate_multi_conf(config):
    flat = nested_to_record(config)
    flat = { tuple(key.split('.')): value for key, value in flat.items()}
    multi_config_flat = { key[:-1] + (key[-1][6:],) : value for key, value in flat.items() if key[-1][:5]=='multi'}
    if len(multi_config_flat) == 0: return # if there are no multi params this generator is empty
    keys, values = zip(*multi_config_flat.items())

    # delete the multi_params
    # taken from https://stackoverflow.com/a/49723101/4841832
    def delete_keys_from_dict(dictionary, keys):
        for key in keys:
            with suppress(KeyError):
                del dictionary[key]
        for value in dictionary.values():
            if isinstance(value, MutableMapping):
                delete_keys_from_dict(value, keys)
    to_delete = ['multi_' + key[-1] for key, _ in multi_config_flat.items()]
    delete_keys_from_dict(config, to_delete)

    for values in itertools.product(*values):
        experiment = dict(zip(keys, values))
        for setting, value in experiment.items():
            reduce(operator.getitem, setting[:-1], config)[setting[-1]] = value
        yield config

Iterating over this with the example above gives:

{'train_config': {'param1': 1, 'param2': [1, 2, 3], 'param3': 2}, 'model_config': {'cnn_layers': [{'units': 3}, {'units': 4}], 'param4': [1, 2]}}
{'train_config': {'param1': 1, 'param2': [1, 2, 3], 'param3': 2}, 'model_config': {'cnn_layers': [{'units': 3}, {'units': 4}], 'param4': [3, 4]}}
{'train_config': {'param1': 1, 'param2': [1, 2, 3], 'param3': 3}, 'model_config': {'cnn_layers': [{'units': 3}, {'units': 4}], 'param4': [1, 2]}}
{'train_config': {'param1': 1, 'param2': [1, 2, 3], 'param3': 3}, 'model_config': {'cnn_layers': [{'units': 3}, {'units': 4}], 'param4': [3, 4]}}
{'train_config': {'param1': 1, 'param2': [1, 2, 3], 'param3': 4}, 'model_config': {'cnn_layers': [{'units': 3}, {'units': 4}], 'param4': [1, 2]}}
{'train_config': {'param1': 1, 'param2': [1, 2, 3], 'param3': 4}, 'model_config': {'cnn_layers': [{'units': 3}, {'units': 4}], 'param4': [3, 4]}}

Which is the result expected.

Any feedback on how to make this code more readable would be very much appreciated!