Programming

Need help to make an algorithm for this problem

Learn Programming - Tue, 06/19/2018 - 07:10

you are given an array of n numbers (a[0],.....,a[n-1]) and an integer k you can change any element of the array to any integer you want. Find the minimum amount of moves you have to spend so that the following condition is satisfied: a[i]-a[i-1] = k For example if you are given an array

[2,5,7,9,85] and k is 2

then it can be changed into [3,5,7,9,11]

and the answer is 2

Any hint would be appreciated

submitted by /u/megatronus8010
[link] [comments]

6 Reasons to Choose Node.js for Web Development

Learn Programming - Tue, 06/19/2018 - 07:04

Node.js has been successful since it was launched. Many developers find it perfect for building fast, multi-user web applications. As you may know, Node is written in JavaScript.That is why it is very easy to learn if you know JavaScript. In this article, you are going to get a list of reasons to choose Node.js.Read full article here.

submitted by /u/simeonplatonov
[link] [comments]

[C++][OOP]Question about the definition of object.

Learn Programming - Tue, 06/19/2018 - 07:03

Hello, I'm learning C++ from Bjarne Stroustrup's : Programming Principles and Practice Using C++ book. The book defines the concept of an object as : "an object s a region of memory with a type that specifies what kind of information can be placed in it".

I usually like to look up definitions of more important concepts from several places. The most used definition of an object i found on the Internet is : an object is an instance of a class.

Now I'm little confused because according to the definition found in the book a variable with a primitive type (for example: int) is an object, while according to the second definition it is not an object.

I would like to ask which is the more commonly accepted definition in C++ and OOP in general? Also is it normal that almost every concept I look up has several different definitions? Should I stop trying to find the generally accepted definition for every programming term and just learn the general idea of the concepts and how to apply them in practice?

Thanks for help and sorry for bad English.

submitted by /u/thecybermarmot
[link] [comments]

How to safely prompt user for input and get inputted data in C?

Learn Programming - Tue, 06/19/2018 - 06:54

I’ve been searching around but haven’t yet found a solid way that’s said to be safe. Anyone?

submitted by /u/OzziePeck
[link] [comments]

Nerual network beginner problem (python)

Learn Programming - Tue, 06/19/2018 - 06:25

I just tried doing my first multilayered network to learn the XOR. The network is made of two input nodes two hidden and one output node.

The program dosen't advande anywhere and after training it it usually answear 0.4 on every input combination. I believe my math is wrong somewhere in the back propagation so it would be helful if someone would check it and explain to me what I did wrong.

Code here

submitted by /u/Gromeer
[link] [comments]

Neural Network beginner problem (Python)

Learn Programming - Tue, 06/19/2018 - 06:13

I'm trying to make a neural network learn the XOR but the program dosent progress and learns instead it just averages out on 0.4 as an answear for every input.

The network is simple with two input nodes two hidden and one output.

And the entire code if someone is willing to read throught it and explain why it learn how to solve the problem. Or if thats too much just read througt the backpropagation where i believe the problem lies the function is called "def backPropagation():". I believe it's my maths that at fault here.

import math, random

#Def XOR

def XOR(x1,x2):

if(x1 != x2):

return 1;

else:

return 0;

#Sigmoid old school

def sigmoid(x):

return 1/ (1+math.exp(-x) );

#The perceptron class

class perceptron(object):

def __init__(self):

self.weight = [random.uniform(-1,1), random.uniform(-1,1), random.uniform(-1,1)];

self.value = 0;

#Creating the perceptrons

p = [];

for i in range(0,2):

p.append(perceptron());

o = perceptron();

#Forwarding in the neural network

def forward():

#Zeroing the values so we dont add old values with the new ones

o.value = 0;

for i in range(0,2):

p[i].value = 0;

#Forwarding

for y in range(0,2):

for x in range(0,3):

p[y].value += inputs[x] * p[y].weight[x];

for i in range(0,2):

p[i].value = sigmoid(p[i].value);

for i in range(0,2):

o.value += p[i].value * o.weight[i];

o.value += o.weight[2] * 1;

o.value = sigmoid(o.value);

#As the name of the function states this is the backpropagation

def backPropagation():

error = XOR(inputs[0], inputs[1]) - o.value;

errorh = [None,None];

errorh[0] = (o.weight[0] / (o.weight[0] + o.weight[1])) * error;

errorh[1] = (o.weight[1] / (o.weight[0] + o.weight[1])) * error;

for i in range(0,2):

o.weight[i] += error * p[i].value * 0.1;

o.weight[2] += error * 0.1;

for y in range(0,2):

for x in range(0,2):

p[y].weight[x] += errorh[y] * inputs[x] * 0.1;

for i in range(0,2):

p[i].weight[2] += errorh[i] * 0.1;

inputs = [None,None,None];

#Supposed to be the training

for i in range(0,10000):

inputs = [random.randint(0,1), random.randint(0,1), 1];

forward();

backPropagation();

print("done");

#This is the manual testing

while True:

inputs = [float(input()),float(input()),1]

forward();

backPropagation();

print(o.value);

submitted by /u/Gromeer
[link] [comments]

Can't get past this incremental integer problem.... C++

Learn Programming - Tue, 06/19/2018 - 06:12

I'm trying to make a very short console program that does 1/1 and then 1/2 and so on for a while with just one loop. I've tried several different methods from googling it and it never works correctly.

I want to set an int to 0 or 1 and then have the program do 1/int, then add 1 to int, and then 1/int again and so on, while printing the results to the screen (as a double AND a float, which seems to work so far). I've tried using different combos of i++ and ++i, but I can't make it work.

I'm currently using a do-while loop, but I can use a while or for loop if it would be easier.

submitted by /u/Elbynerual
[link] [comments]

What Is Serverless?

Programming - Tue, 06/19/2018 - 05:52
Categories: Programming

Blockers to IPv6 Adoption

Programming - Tue, 06/19/2018 - 05:46
Categories: Programming

What could be the flaw in the following code for finding the maximum sum of a contiguous subarray problem?

Learn Programming - Tue, 06/19/2018 - 05:43

It works for some test cases but fails on a particular one. I have included the test case in the comment section. My code evaluates the max sum as 98 against the expectation of 217.

int Solution::maxSubArray(const vector<int> &A) { int hi_sum = 0; int cur_sum = 0; int j = A.size() - 1; int i = 0; for(int k = 0; k<A.size(); k++) { cur_sum += A[k]; } hi_sum = cur_sum; while(i != j) { if(A[i] < A[j]) { cur_sum -= A[i]; i++; } else { cur_sum -= A[j]; j--; } if(hi_sum < cur_sum) hi_sum = cur_sum; // cout<<"I - "<<i<<'\n'<<"J - "<<j<<'\n'<<"Sum - "<<hi_sum<<'\n'<<"Cur Sum - "<<cur_sum<<'\n'; } return hi_sum; }

It fails on the following test case -

{-120, -202, -293, -60, -261, -67, 10, 82, -334, -393, -428, -182, -138, -167, -465, -347, -39, -51, -61, -491, -216, -36, -281, -361, -271, -368, -122, -114, -53, -488, -327, -182, -221, -381, -431, -161, -59, -494, -406, -298, -268, -425, -88, -320, -371, -5, 36, 89, -194, -140, -278, -65, -38, -144, -407, -235, -426, -219, 62, -299, 1, -454, -247, -146, 24, 2, -59, -389, -77, -19, -311, 18, -442, -186, -334, 41, -84, 21, -100, 65, -491, 94, -346, -412, -371, 89, -56, -365, -249, -454, -226, -473, 91, -412, -30, -248, -36, -95, -395, -74, -432, 47, -259, -474, -409, -429, -215, -102, -63, 80, 65, 63, -452, -462, -449, 87, -319, -156, -82, 30, -102, 68, -472, -463, -212, -267, -302, -471, -245, -165, 43, -288, -379, -243, 35, -288, 62, 23, -444, -91, -24, -110, -28, -305, -81, -169, -348, -184, 79, -262, 13, -459, -345, 70, -24, -343, -308, -123, -310, -239, 83, -127, -482, -179, -11, -60, 35, -107, -389, -427, -210, -238, -184, 90, -211, -250, -147, -272, 43, -99, 87, -267, -270, -432, -272, -26, -327, -409, -353, -475, -210, -14, -145, -164, -300, -327, -138, -408, -421, -26, -375, -263, 7, -201, -22, -402, -241, 67, -334, -452, -367, -284, -95, -122, -444, -456, -152, 25, 21, 61, -320, -87, 98, 16, -124, -299, -415, -273, -200, -146, -437, -457, 75, 84, -233, -54, -292, -319, -99, -28, -97, -435, -479, -255, -234, -447, -157, 82, -450, 86, -478, -58, 9, -500, -87, 29, -286, -378, -466, 88, -366, -425, -38, -134, -184, 32, -13, -263, -371, -246, 33, -41, -192, -14, -311, -478, -374, -186, -353, -334, -265, -169, -418, 63, 77, 77, -197, -211, -276, -190, -68, -184, -185, -235, -31, -465, -297, -277, -456, -181, -219, -329, 40, -341, -476, 28, -313, -78, -165, -310, -496, -450, -318, -483, -22, -84, 83, -185, -140, -62, -114, -141, -189, -395, -63, -359, 26, -318, 86, -449, -419, -2, 81, -326, -339, -56, -123, 10, -463, 41, -458, -409, -314, -125, -495, -256, -388, 75, 40, -37, -449, -485, -487, -376, -262, 57, -321, -364, -246, -330, -36, -473, -482, -94, -63, -414, -159, -200, -13, -405, -268, -455, -293, -298, -416, -222, -207, -473, -377, -167, 56, -488, -447, -206, -215, -176, 76, -304, -163, -28, -210, -18, -484, 45, 10, 79, -441, -197, -16, -145, -422, -124, 79, -464, -60, -214, -457, -400, -36, 47, 8, -151, -489, -327, 85, -297, -395, -258, -31, -56, -500, -61, -18, -474, -426, -162, -79, 25, -361, -88, -241, -225, -367, -440, -200, 38, -248, -429, -284, -23, 19, -220, -105, -81, -269, -488, -204, -28, -138, 39, -389, 40, -263, -297, -400, -158, -310, -270, -107, -336, -164, 36, 11, -192, -359, -136, -230, -410, -66, 67, -396, -146, -158, -264, -13, -15, -425, 58, -25, -241, 85, -82, -49, -150, -37, -493, -284, -107, 93, -183, -60, -261, -310, -380} submitted by /u/Ankur_Satya
[link] [comments]

Set cookies within my jQuery code

Learn Programming - Tue, 06/19/2018 - 05:19

Hi,

I have some code here, which changes the layout view on my page, I want to use cookies to remember the choice but I'm not sure how to do it, here's the code:

<script> $(document).ready(function(){ $('#three-view').click(function(){ $('.col-md-2').addClass('col-md-4'); $('.col-md-4').removeClass('col-md-2'); }); $('#six-view').click(function(){ $('.col-md-4').addClass('col-md-2'); $('.col-md-2').removeClass('col-md-4'); }); }); </script> submitted by /u/t0mni
[link] [comments]

How to solve this error I am getting in assembly language programming ? screenshot and code included

Learn Programming - Tue, 06/19/2018 - 05:10

.model small

.data

s1 db 13,10,"Enter the value of n","&"

s2 db 13,10,"Enter the number:","&"

s3 db 13,10,"Reverse pattern:","&"

n1 dw ?

.code

extrn readint:near,writeint:near

main proc

mov ax,@data

mov ds,ax

mov dx,offset s1

mov ah,09h

int 21h

call readint

mov cx,ax

mov n1,ax

mov dx,offset s2

mov ah,09h

int 21h

next1:

call readint

push ax

loop next1

mov dx,offset s3

mov ah,09h

int 21h

mov cx,n1

next2:

pop ax

call writeint

loop next2

mov ah,04h

int 21h

endp

end

screenshot of the error when linking :

https://imgur.com/a/Xd7aqNs

submitted by /u/millionairesai
[link] [comments]

What is consistent hashing and how to implement it efficiently?

Learn Programming - Tue, 06/19/2018 - 04:50

In this article, we’ll understand what consistent hashing is all about and why it is an essential tool in scalable distributed system architectures. Further, we’ll look at data structures that can be used to implement this algorithm efficiently at scale. In the end, we’ll also have a look at a working example for the same.

https://blog.ably.io/how-to-implement-consistent-hashing-efficiently-fe038d59fff2

submitted by /u/atanasovd
[link] [comments]

Pages