18 Eylül 2020 Cuma

Excel Notlarım

User Defined Function
Açıklaması şöyle
User Defined Functions (UDFs) are very easy to install and use:

1. ALT-F11 brings up the VBE window
2. ALT-I ALT-M opens a fresh module
3. paste the stuff in and close the VBE window
If you save the workbook, the UDF will be saved with it. If you are using a version of Excel later then 2003, you must save the file as .xlsm rather than .xlsx

To remove the UDF:

1. bring up the VBE window as above
2. clear the code out
3. close the VBE window
Örnek
Şöyle yaparız
Public Function noemail(s As String) As String
    Dim i As Long, L As Long

    If InStr(s, "@") = 0 Then
        noemail = s
        Exit Function
    End If
    
    L = Len(s)
    For i = L To 1 Step -1
        If Mid(s, i, 1) = "," Then
            noemail = Left(s, i - 1)
            Exit Function
        End If
    Next i
    
End Function
Kullanmak için şöyle yaparız
=noemail(A1)
Data Menüsü
Remove Duplicates
Seçili sütunlardaki çift değerleri siler.

Tabloya Çevirmek
Şöyle yaparız.
Turn the data into an Excel Table like this
1.select the data and hit Ctrl - T
2.select the data and click Insert > Table
3.Tick the box for "My table has headers".
Now you can use the drop-down commands in each table header to sort (and/or filter) by that column.

View Menüsü
Freeze Pane
Bazı satırların hareket etmemesini sağlar.

F4
Bir satırı en soldaki numarasına sağ tıklayıp sil. Silme işlemi hatırlanır. Başka satıra gidip F4'e basarsam o satır da silinir.

Replace
Ctrl+h ile kutusu açılır. Kullanırken "Match case" ve "Match Entire Cell Content" seçilebilir.

Fonksiyonlar
Addres
Açıklaması şöyle
You can use the ADDRESS function to obtain the address of a cell in a worksheet, given specified row and column numbers. For example, ADDRESS(2,3) returns $C$2. As another example, ADDRESS(77,300) returns $KN$77. You can use other functions, such as the ROW and COLUMN functions, to provide the row and column number arguments for the ADDRESS function.

Syntax: ADDRESS(row_num, column_num, [abs_num], [a1], [sheet_text])
And
TRUE ve FALSE alır. Sonuç olarak TRUE, FALSE döner.
=IF(AND(R1,S1),"Yes","No")

COUNTIF
Verilen koşulu sağlayan hücrelerin sayısını verir. Örnek a harfini içeren hücreleri sayıyor.
=COUNTIF(A1:A7,"*a*")
FILTER
Şöyle yaparız. Açıklaması şöyle. A1:A5 arasında 5'ten küçük sayıların sayısını verir
Separating conditions by * will act as AND, while separating them by + will work as OR.
=COUNT(FILTER(A1:A5,(A1:A5<5) + (B1:B5<5)))
IF
Verilen koşulu sağlarsa çıktı olarak ilk değeri sağlamazsa ikinci değeri verir.
=IF(SUM(B2:B30)>0,1,0)
LEFT
Örnek
Şöyle yaparızz
=LEFT(D2,LEN(B2))
Örnek
Şöyle yaparız. Soldaki iki karakteri al. Sağdaki geri kalan karakterleri alır. Sağdaki kısmı sayı yapar yani baştaki sıfırları atarr ve iki değeri birleştirir.
=LEFT(A1,2) & VALUE(RIGHT(A1,LEN(A1)-2))
SUBSTITUTE
Örnek
Şöyle yaparız. Virgül yerine "" yazar..
=SUBSTITUTE(A1,",","")
TEXTJOIN - Delimiter, Ignore Empty + Text

Örnek
Şöyle yaparız. Burada Filter'a verilen ilk parametre verinin aralığı, ikinci parametre key değerlerin aralığı, Key değerleri E2 ile aynı olanları döndürür. Bu değerleri TEXTJOIN ile birleştiririz
=TEXTJOIN(", ",TRUE,FILTER($B$2:$B$7,$C$2:$C$7=$E2))
Elimizde şöyle bir veri olsun
| ID | Status   |
|----|----------|
|  1 | Complete |
|  2 | Active   |
|  3 | New      |
|  4 | New      |
|  5 | New      |
|  6 | Complete |
Çıktı olarak şunu alırız
| Status   | IDs     |
|----------|---------|
| Complete | 1, 6    |
| Active   | 2       |
| New      | 3, 4, 5 |
VLOOKUP
Örnek
Şöyle yaparız.
=VLOOKUP($B81,sheet2!$A$2:$M$281,2,FALSE) 
- Birinci parametre aranılan değerdir. $B81 aranılan değeri belirtir.
- İkinci parametre üzerinde arama yapılacak tablodur. sheet2!$A2:$M281 tabloyu belirtir.
- Üçüncü paramtre tablodaki bir satırda istenilen değer bulunursa hangi sütunun sonuç olarak bana döndürüleceğini belirtir. Bu örnekte sonuç olarak ikinci sütunu istiyoruz
- Dördünce parametre aranılan değerin tam olarak veya kısmi olarak bulunucağını belirtir. Açıklaması şöyle. Bu örnekte tam eşleşme bekleniyor.
The last argument of the VLOOKUP function determines the nature of your look up.

1 - TRUE will return an approximate match which is useful when you want to look up a value from group of ranges in ascending order;

0 - FALSE will return an exact match which will return the first match based on your look up criteria.



17 Eylül 2020 Perşembe

Internet Control Message Protocol - ICMP

ICMP Nedir ?
Her IP Stack'inde ICMP protokolü bulunur. Açıklaması şöyle
ICMP, uses the basic support of IP as if it were a higher level protocol, however, ICMP is actually an integral part of IP, and must be implemented by every IP module.
ICMP , TCP ve UDP kardeş protokollerdir. Ancak TCP ve UDP'nin aksine ICMP protokolünde port numarası kavramı yoktur.

ICMPv4 Header
ICMPv4 mesajının yapısı şöyle.
/* ICMP Header */
struct icmpheader {
  unsigned char icmp_type; //ICMP message type
  unsigned char icmp_code; //Error code
  unsigned short int icmp_chksum; //Checksum for ICMP Header and data
  unsigned short int icmp_id; //Used in echo request/reply to identify request
  unsigned short int icmp_seq;//Identifies the sequence of echo messages, 
                    //if more than one is sent.
};
icmp_type Alanı
icmp_type alanınn alabileceği değerler ve açıklaması şöyle.
char * messages[ ] = {
   "Echo reply", //0
   "Type 1",     //1
   "Type 2",     //2
   "Destination unreachable",//3
   "Source quench",  //4
   "Redirect",       //5
   "Type 6",         //6
   "Type 7",         //7
   "Echo request",   //8
   "Router advertisement", //9
   "Router discovery",  //10
   "Time exceeded",     //11
   "Parameter problem", //12
   "Timestamp request", //13
   "Timestamp reply",   //14
   "Information request (obsol.)", //15
   "Information reply (obsol.)",   //16
   "Address mask request",         //17
   "Address mask reply"            
};
Eğer ICMP hata ile ilgiliyse gönderilen paketin ilk 64 byte'ını da içerir. Açıklaması şöyle.
An ICMP error message will contain the IP header and first 64 bits (eight octets) of the original data
Bu 64 byte kullanılarak orijinal paket bulunabilir. Açıklaması şöyle.
Internet Header + 64 bits of Data Datagram

The internet header plus the first 64 bits of the original datagram's data. This data is used by the host to match the message to the appropriate process. If a higher level protocol uses port numbers, they are assumed to be in the first 64 data bits of the original datagram's data.
Örneğin icmp_type Alanı = 11 (Time exceeded) icmp_code = 0 (net unreachable) ise bu 64 byte'ı cevapta geri alırız.

icmp_code Alanı
Alabileceği değerler şöyle
0 = net unreachable;

1 = host unreachable;

2 = protocol unreachable;

3 = port unreachable;

4 = fragmentation needed and DF set;

5 = source route failed.
Açıklaması şöyle.
Codes 0, 1, 4, and 5 may be received from a gateway. Codes 2 and 3 may be received from a host.
ICMP Router Advestisement
ICMP Echo -> ICMP Reply mesajları yanında ICMP mesajı "ICMP Router Advestisement" için de gönderilebilir.
ethertype IPv4 (0x0800), ..., proto ICMP (1), length 36)
  192.168.161.101 > 224.0.0.1: ICMP router advertisement...
ICMPv6 Header
Linux'ta şu satırı dahil ederiz.
#include <netinet/icmp6.h>
ICMPv6 8 byte büyüklüğündedir. ICMPv6 mesajının yapısı şöyle
struct icmpv6header {
  unsigned char type;
  unsigned char code;
  unsigned short int chk_sum;
  unsigned int body; 
};
ICMP Engelleme
ICMP engellenebilir veya işlenme hızı azaltılabilir. IPV6 için açıklaması şöyle
For example RFC4890 says the following types should be allowed for optimal functionality:
  Type 1, 2, 3, 4, 128, 129 and for mobility assistance also 144, 145, 146 and 147.
ICMP Redirect
Açıklaması şöyle.
ICMP re-directs are most often seen when you have a host or router A in the same subnet with two other routers B & C and connectivity to both.
...
A will have a route (most likely a default) pointing to B, and B will have a more specific route pointing to C.

Without ICMP redirects, all traffic from A to C will be routed A->B->C

With ICMP re-direct enabled, B will inform A that C is a better next-hop and subsequent traffic will be routed A->C.

16 Eylül 2020 Çarşamba

GoF - Composite Örüntüsü

Composite - Yapısal Örüntü
Composite örüntüsü parça-bütün ilişkisine sahip ağaç yapılarıdır. Yapıdaki tüm nesnelere aynı işlemi kolayca uygulamayı sağlar.
"A composite contains components. Components come in two flavors : composites and leaf elements."
Örüntüdeki Bileşenler
Açıklaması şöyle
Below is the list of classes/objects used in the composite pattern, which has four :

Component – Component is the interface (or abstract class) for the composition of the objects and methods for accessing/processing its child or node components. It also implements a default interface to define common functionalities/behaviors for all component classes.

Leaf – The leaf class defines a concrete component class, which does not have any further composition. The leaf class implements the component interface. It performs the command/task at its end only.

Composite – The composite class defines a concrete component class, which stores its child components. The composite class implements the component interface. It forwards the command/task to the composite objects it contains. It may also perform additional operations before and after forwarding the command/task.

Client – The client class uses the component interface to interact/manipulate the objects in the composition (Leaf and Composite).
Hiyerarşi
Yapısal olarak şöyledir. Composite Base nesnenin listesini içerir.
Base
^
|
Composite --(has) List of Base
Klasik Örnek
GUI'de en üstte bulunan Frame/Panel gibi bir nesne diğer nesneleri içerir. Ana bileşene show() komutu gönderilince içerdiği diğer tüm bileşenlere de aynı komutu gönderir.

İş Yükü Dağıtımı -  Work Load Distribution
Elimizde şöyle bir arayüz olsun
public interface Worker {
  void assignWork(Employee manager, Work work);
  void performWork();
}
Bu arayüzü gerçekleştiren şöyle sınıflar olsun
public abstract class Employee implements Worker {
  ...
}
public class Engineer extends Employee {
  private List<Work> works = new ArrayList<>();
  ...
  @Override
  public void assignWork(Work work) {
    this.works.add(work);
  }
  @Override
  public void performWork() {
  works.stream().forEach(work -> {...});
  works.clear();
  }
}

public class Manager extends Employee {
  List<Employee> managingEmployees = new ArrayList<>();

  public boolean manages(Employee employee) {
    return managingEmployees.add(employee);
  }
  @Override
  public void assignWork(Work work) {
    //Tüm işleri astlara eşit böl ve ata
  }
  @Override
  public void performWork() {
    managingEmployees.stream().forEach(employee -> employee.performWork());
  }
}
En üst yöneticiye işleri atadıktan sonra tüm astlara işlerin dağıtan kod şöyledir.
public static void main(String[] args) {
  Engineer ajay =...;
  Engineer vijay =...;
  Engineer jay =...;
  Engineer martin =...;
  Manager kim =...;
  Engineer anders = ...;
  Manager niels = ...;
  Engineer robert = ...;
  Manager rachelle = ...;
  Engineer shailesh = ...;
  kim.manages(ajay);
  kim.manages(martin);
  kim.manages(vijay);
  niels.manages(jay);
  niels.manages(anders);
  niels.manages(shailesh);
  rachelle.manages(kim);
  rachelle.manages(robert);
  rachelle.manages(niels);
  rachelle.assignWork(...);
  rachelle.performWork();
}
Mesaj Örneği
XML yapısını Composite olarak okuyan bir yapı olsun. Her nesne FieldDefinition arayüzünden kalıtır.
RecordDefiniton has --> AlternativeFieldDefinition
                                  --> NormalFieldDefinition
                                  --> SpareFieldDefinition
Daha sonra bu definition'ları bir composite mesaj haline getirelim.Her bir field kendi içinde FieldDefinition yapısını içerir Her nesne CompositeData ata sınıfından kalıtır.
GenericRecord has --> AlternativeField
                                --> NormalField
                                --> AdaptorField
Böylece stream'e okuma yazma yapabilen generic bir composite elde ederiz.

Bir başka örnek
Önce bir ata sınıf veya arayüz yaratılır. Vehicle isimli bir sınıfımız olsun
public abstract class Vehicle
{
  public void drive(int distance) {
    System.out.println("Drove " + distance + ". Vroom vroom!");
  }

  public abstract int getWeight();
  // etc.
}
Daha sonra ata sınıfı gerçekleştiren başka sınılar yazılır.
public class Car extends Vehicle
{
  protected int weight;

  // ...

  @Override
  public int getWeight() {
    return this.weight;
  }
}
Son olarak bu sınıflar bünyesinde barındıran bir composite yaratılır.
public class CarCarrier extends Vehicle
{
  protected int weight;
  protected Vehicle[] cars;

  // ...

  @Override
  public int getWeight() {
    int weight = this.weight;
    for(Vehicle car : cars) {
      weight += car.getWeight();
    }
    return weight;
  }
}
Örnek
Elimizde şöyle bir arayüz olsun. Burada bir composite içinde olabilecek root, leaf ve ara düğümler gösteriliyor.
interface Human {
  void addChild(Human child);
  void addParent(Human parent);
}
Elimizde şöyle sınıflar olsun
public class GrandParent implements Human {

  private List<Human> children = new ArrayList<>();

  @Override
  public void addChild(Human child) {
    childs.add(child);
  }
  @Override
  public void addParent(Human parent) {
  }
}
public class Parent implements Human {
  private List<Human> children = new ArrayList<>();
  private List<Human> parents  = new ArrayList<>();

  @Override
  public void addChild(Human child) {
    children.add(child);
  }
  @Override
  public void addParent(Human parent) {
    parents.add(parent);
  }
}
public class Child implements Human {

  private List<Human> parents = new ArrayList<>();
   
  @Override
  public void addChild(Human child) {
  }
  @Override
  public void addParent(Human parent) {
    parents.add(parent);
  }
}
Şöyle yaparız
Child child = ...
Parent father = ...
Parent mother = ...

child.addParent(father);
child.addParent(mother);
father.addChild(child);
mother.addChild(child);

GrandParent granpa = ...;

granpa.addChild(father);
father.addParent(granpa);
Zor Tarafları

Composite veri yapısını dolaşması (traverse) işlemi zor olabiliyor.